Yes, the System.Diagnostics.TraceListener class. You will need to define the TRACE constant for it to work, but you can use several built-in tracelisteners using the configuration of your app.config:
app.config looks something like this, if you want to write to a file, there are many filters that you can also add:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="yourName" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\mylogfile.txt" /> </listeners> </trace> </system.diagnostics> </configuration>
And use:
Trace.TraceError("There been an error captain: {0}", e); Trace.TraceWarning("The system broke but don't worry."); Trace.TraceInformation("Starting up the engines.");
Personally, I would not write to a text file, if you can avoid it, the event log is better located, because you can sort, filter, the logs are automatically cleared, and you do not get problems with blocking files.
source share