Source Viewer: SyslogTraceListener.cs
Font Size:
1
2
3
4
5
6
7
MorphArray.cs
Morpho.cs
MyWebClient.cs
PronounceWord.cs
syslog.cs
SyslogTraceListener.cs
MQ
..
// SyslogTraceListener.cs // // This code illustrates how to implement a Syslog-based TraceListener. // // Within .NET, there is the concept of a TraceListener. This // is alayer of code that "listens" for trace messages and does // something with them. There are examples of TraceListeners // that dump trace messages to the console, to a text file, to a WinForms // ListBox, or to SQL server for post-processing and analysis. // // This example shows how to implement a TraceListener that // dumps its messages to the specified syslog server. // // To test it, you need a syslog server. There is a freeware syslog server // for Win32 available from Kiwi, http://www.kiwisyslog.com/ . // Or, you can use syslogd on any Unix. // // -------------------------------------------------------- // How to use this code: // It should be compiled into a DLL via something like: // csc.exe /t:library /debug+ /out:SyslogTraceListener.dll SyslogTraceListener.cs // // It can then be used in any app: console app, windows service, // ASP.NET page, webservice, Win Forms app, or etc. // // There are two mechanisms for registering a TraceListener in a // .NET application : // a. via the application config file // b. via application code // // In the former case, the TraceListener is registered at // runtime, so there is no reference to the TraceListener in // application code. You do not need to link your app with this // DLL. However, the DLL must be placed in the applications // search directory. In ASP.NET, this is the bin dir. In // WinForms or a Console app, it should be the same dir as the // exe. Or for any app, this DLL can be placed in the GAC. // // The config-file syntax to register the listener is: // //
//
//
//
//
//
//
//
//
// // The "initializeData" specifies the name or IP Address of the syslog host. // The "name" of the trace listener is immaterial. A simple test app would be: // // #define TRACE // // namespace Ionic { // // public class TestClient { // // public static void Main(string[] args) { // System.Diagnostics.Trace.Write("Hello!"); // System.Diagnostics.Trace.Write("Goodbye!"); // } // } // } // // // In the latter case, you can include the following in your // application code (at startup time): // // Ionic.SyslogTraceListener listener= new Ionic.SyslogTraceListener("localhost"); // System.Diagnostics.Trace.Listeners.Add(listener); // // // In both cases, you need to compile the .NET application with // the TRACE directive defined. For C#, use /d:Trace on the // csc.exe command line, or #define TRACE in the source file . // For VB, it is different. // // If you do BOTH (config file and in app code) then there will // be two distinct listeners created and they will both log messages. // // ======================================================== // (c) Ionic Shade // Tue, 02 Nov 2004 09:29 // using System; using System.Net; // for IPHostEntry, IPEndPoint, etc using System.Net.Sockets; // for UdpClient using System.Diagnostics; using System.Reflection; // for MethodInfo, MethodBase, etc namespace Ionic { namespace Syslog { public enum Level { Emergency= 0, Alert=1, Critical=2, Error=3, Warning=4, Notice=5, Information=6, Debug=7, } public enum Facility { Kernel=0, User=1, Mail=2, Daemon=3, Auth=4, Syslog=5, Lpr=6, News=7, UUCP=8, Cron=9, Local0=10, Local1=11, Local2=12, Local3=13, Local4=14, Local5=15, Local6=16, Local7=17, } } public class SyslogTraceListener : System.Diagnostics.TraceListener { UdpClient c; int syslogPort= 514; public SyslogTraceListener( string host ) { IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 0); c = new UdpClient(ipLocalEndPoint); c.Connect(host,syslogPort); } public override void Close() { c.Close(); base.Close (); } // a more sophisticated Listener might buffer Writes and flush later. // this implementation just emits each Write as a syslog message. public override void Write(string message) { try { // TODO: optionally exclude Filename and line number info // (it is expensive to collect) // to find the source stackframe, we have to walk backwards: StackTrace stackTrace = new StackTrace(true); // collect line# and filename etc StackFrame stackFrame; MethodBase mb; int frameCount = 0; string typeName; do { frameCount++; stackFrame = stackTrace.GetFrame(frameCount); mb = stackFrame.GetMethod(); typeName = mb.ReflectedType.FullName; } while ( typeName.StartsWith("System") || typeName.EndsWith("SyslogTraceListener") ); // we have the stackframe that originated the trace message. // TODO: Here, the syslog Facility and Level is hard-coded. This could be parameterized. string syslogMsg= System.String.Format("<{0}>[{1}.{2}] [{3}:{4}] {5}", (int)Ionic.Syslog.Facility.User*8 + Ionic.Syslog.Level.Information, typeName, mb.Name, stackFrame.GetFileName(), stackFrame.GetFileLineNumber(), message); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(syslogMsg); c.Send(bytes, bytes.Length); } catch (Exception ex1) { System.Console.WriteLine("Cannot get stackframe info: {0}", ex1); } } public override void WriteLine(string message) { this.Write(message); } } }
The srcview page has been enjoyed 356544 times since 18 September 2003