Click to See Complete Forum and Search --> : How to use Trace


mce
February 12th, 2009, 07:20 AM
I want to use trace to file.


FileStream Log = new FileStream("Log.txt", FileMode.OpenOrCreate);

Trace.Listeners.Add(new TextWriterTraceListener(Log));
Trace.WriteLine("My Trace String To Log File");
Trace.Flush();
Log.Close();



The problem with the above codes is trace will always log to the file Log.txt.
How to call Trace with the file handler itself??, so that i can specify which file to log to when i do a Trace.

I tried using traceSource like the following, but it doesn';t work as well., the information is not even logged into file!!

{
string l_path = String.Format("c:\\mylog_{0}.txt", this.GetHashCode());
_log = new FileStream(String.Format(l_path), FileMode.Append);

}
if (_traceSrc == null)
{
_traceSrc = new TraceSource(this.GetHashCode().ToString());
_traceSrc.Listeners.Add(new TextWriterTraceListener(_log));
_traceSrc.TraceInformation(String.Format("Hello from {0}", GetHashCode()));

eclipsed4utoo
February 12th, 2009, 09:38 AM
so you want to be able to log to multiple log files?

mce
February 12th, 2009, 06:40 PM
yes..

boudino
February 13th, 2009, 02:46 AM
Same log in every file, or some information to one file and some to another? Than I'd recommend you to use some kind of logging framework, e.g. Logging Application Block of Microsoft's Enterprise Library.