Trace object in .NET has a default trace listener and out put to this default trace listener can be viewed by using debugview utility from Microsoft ( sysinternals). It is also possible to direct the out put from Trace object to any other listener. Listener means any text file or Console or even log where we can write some text messages.
//Following line will create a text file ( MyLogs.txt ) in the same directory where the application //is running.
private static System.Diagnostics.TextWriterTraceListener TL = new TextWriterTraceListener(Environment.CurrentDirectory + "\\MyLogs.txt");
//Add this listener to the Trace object
Trace.Listeners.Add(TL);
//Write information to the txt file
Trace.WriteLine("Some Informational Message here");
This is a powerful technique to write logs to a specified txt file. The TL switch can be used to set the Trace level and also to enable or disable the trace.
- Abhinaw
No comments:
Post a Comment