tracing
Hi
I would like to trace my application and put all messages in a text file.
I use
Trace.TraceError("");
Trace.TraceInformation("");
Trace.TraceWarning("");
methods and when TraceSwitch level is set to for instance Error all messages are put in a file?
Why?
I thought when I set TraceSwitch (ts) to Error only TraceError messages would be sent.
I would like to change only trace level and certain messages should autmatically be put in a file.
Should I use Trace.WriteIf(ts.TraceError == true,"...");
What for if TraceError, TraceWarning etc.?
When TraceLevel is set in config file everything is fine however when in the code it doesn't work.
Ela
[743 byte] By [
e_LA] at [2007-12-25]
You are confusing the Trace class with TraceSwitch and TraceSource. The Trace and TraceSwitch classes were available in v1.x and traced information to any registered listeners irrelevant of any trace settings you may have. If you wanted to use the switch to control tracing you'd use a conditional on the switch.
In v2.0 you define a TraceSource from which to generate trace events. In the source you either define the source levels to trace or you can even specify the exact switch to use.
TraceSource m_TraceSource = new TraceSource("MySource", SourceLevels.Error);
m_TraceSource.TraceEvent(TraceEventType.Warning, 0, "Warning message, only if the switch shows warnings.");
You don't need a TraceSwitch in this case but you could define one anyway.
Michael Taylor - 10/2/06
Thanks for the reply. I put these line of code in my test program.
What is wrong below. It doesn't work. There is no output.log file.
TraceSource m_TraceSource = new TraceSource("MySource", SourceLevels.Error);TextWriterTraceListener MyListener = new TextWriterTraceListener("output.log");m_TraceSource.Listeners.Add(MyListener);
m_TraceSource.TraceEvent(
TraceEventType.Warning, 0, "Warning message, only if the switch shows warnings.");
If I change like that
TextWriterTraceListener(Console.Out)
it doesn't work either.