Archive

Posts Tagged ‘logs’

Log files and TextWriterTraceListener

February 1, 2012 2 comments

Writing a log file using TraceListner classes its just one more way of writing logs..  🙂

private int Logger(Exception exObj)
{
using (Stream myFile = File.Exists(“TestFile.txt”) ? File.Open(“TestFile.txt”, FileMode.Append) : File.Create(“TestFile.txt”))
{
/* Create a new text writer using the output stream, and add it to * the trace listeners. */
TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);
// Write output to the file.
Trace.Write(“\r\n Test output \r\n”);
Trace.Write(exObj);
Trace.Write(“\r\n Test output \r\n”);

// Flush the output.
Trace.Flush();
myFile.Close();

Trace.Listeners.Remove(myTextListener); // this is very important else you may encounter error wrt file/stream/trace object
myFile.Dispose();
}
return 0;
}

Advertisements
Categories: General Tags: , ,