Best practise with Trace

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've been looking at the Trace and TraceSwitch Classes i C# and I thrying to make it act more like java log4j.
What I want is to add two different tracelisteners (like EventLogTraceListener and TextWriterTraceListener) to the Trace class. I also want them to react on different TraceSwitch Levels. I dont want the windows eventlog to log anything else than errors. The textLog will log on the verbose level. Is there anyone here that knows how to do this

My point is that I want to be able to write ex. Trace.writeline(myTraceSwitch.TraceWarn,"My log Warning"); in the applikation code. The applikation will check for the switchlevel in myApp.exe.config and then write to the textlog if the config level was set to warn,info, or verbose. If the level was set to Error it would also write to the eventlog

Jens Aleniu
 
Hi
You might need to define two trace switch if you want to write two
different level
You define a trace switch on the web.config as so
<system.diagnostics>
<switches>
<add name="Tracing_one" value="0" />
</switches>
</system.diagnostics>
Then you can set the level like so , you said you need one to use one for
error and the other for verbose
So the error will be like so
<add name="Tracing_one" value="1" />
While the other will be
<add name="Tracing_two" value="4" />
Then you use them inside your code as follows
If Tracing_one.TraceError Then
Trace.WriteLine("Error message");


But the bad news is still this will write into either to both logs or non
of them . since they are both added to the same (one and only ) trace
object . what you can do is keep adding an removing the one that will not
be used to the traceListener of the trace object so you keep only the one
that you want to write to added at the moment you write , then you use the
flush method to make sure that the text was already written before you add
the one you removed
So if you want to write to both at a moment add both
Trace.Listeners.Add(theeventone);
Trace.Listeners.Add(thefileone);
But if at some moment you want to write only to file one , not to the win
log one , then remove that one
Trace.Listeners.Remove(theeventone);
Hope that helps




Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
you are welcomed Jens
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Back
Top