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()));
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()));