You can connect to the trace structure, which is part of the CLR. Using a simple class: http://www.chaosink.co.uk/files/tracing.zip , you can selectively record diagnostic information. To use it, add a class to your application. Create a patron protection in your class, for example:
private Tracing trace = new Tracing("My.Namespace.Class");
and call it using:
MyClass() { trace.Verbose("Entered MyClass"); int x = 12; trace.Information("X is: {0}", x); trace.Verbose("Leaving MyClass"); }
The built-in trace structure has 4 levels of information:
Verbose - for registering a program stream
Information - to register specific information of interest to monitors
A warning. To register an invalid state or a recoverable exception.
Error - to register a fatal exception or condition
To access information from your application, add the following to app.config (or web.config):
<system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\mylogfile.log" /> </listeners> </trace> <switches> <add name="My.Namespace.Class" value="4"/> </switches> </system.diagnostics>
You can also connect listeners to publications in the event log or in any other place that interests you. More information about the trace structure can be found at:
http://msdn.microsoft.com/en-us/library/ms733025.aspx
source share