I'd like to use the System.Diagnostics.Trace class to perform simple tracing of my application in a text file. The reason I want to use this and not log4net or any other solution, it is because ... it has to be simple :)
So, I got this working in C#:
TextWriterTraceListener listener = new TextWriterTraceListener(Server.MapPath("~/App_Data/log.txt"));
listener.Filter = new EventTypeFilter(SourceLevels.Warning);
Trace.Listeners.Add(listener);
Or its equivalent in the Web.config:
<system.diagnostics>
<sharedListeners>
<add name="log" type="System.Diagnostics.TextWriterTraceListener" initializeData="App_Data/log.txt">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
</add>
</sharedListeners>
<trace autoflush="true">
<listeners>
<clear/>
<add name="log"/>
</listeners>
</trace>
</system.diagnostics>
So when I execute this:
Trace.TraceInformation("info");
Trace.TraceWarning("warning");
Trace.TraceError("error");
I got the lines about warning and error, that is what I was expecting. The problem is, how do I use the rest of levels that appears in the SourceLevels enum?
Is there any drawback or using System.Diagnostics.Trace for web applications? (autoflush is set to true for testing purposes only)
Cheers.