Home > General > Log files and TextWriterTraceListener

Log files and TextWriterTraceListener


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: , ,
  1. December 10, 2012 at 12:55 am

    Hey would you mind stating which blog platform you’re working with? I’m planning to start my own
    blog in the near future but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something
    unique. P.S My apologies for getting off-topic but I had to ask!

    • manishrao18
      December 10, 2012 at 4:26 pm

      wordpress is my all time favorite!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: