Bookmark and Share

Friday, July 23, 2010

Simply write logs from your .net app using System.Diagnostics.Trace

Here is one of the simplest method for logging from your application, simply use the System.Diagnostics.Trace class and call:

System.Diagnostics.Trace.WriteLine(message);

and add to the App.Config file this section:

<?xml version="1.0"?>
<
configuration>
<
system.diagnostics>
<
trace autoflush="false" indentsize="4">
<
listeners>
<
add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="MyService.log" />
<
remove name="Default" />
</
listeners>
</
trace>
</
system.diagnostics>
</configuration>

And the logging will be executed succesfully.


If you need the logs on the Event Log add:

<add name="myEventListener" type="System.Diagnostics.EventLogTraceListener"  initializeData="TraceListenerLog" />

And you’ll find the event log populated with your application log.