The first part contains the C# code that writes trace events to TextWriterTraceListener, ConsoleTraceListener and EventLogTraceListener. The second part does exactly the some thing but using configuration file to achieve it.
-----------
Part 1
-----------
//using System;
//using System.Diagnostics;
private static void TraceTest()
{
TraceSource ts = new TraceSource("TestConsole");
SourceSwitch sw = new SourceSwitch("testSwitch");
//the level determines what events are sent to listerners,
//currently events level of Information and up.
sw.Level = SourceLevels.Information;
ts.Switch = sw;
//Remove the default DefaultTraceListener
ts.Listeners.Clear();
TextWriterTraceListener twtl= new TextWriterTraceListener(@"testLog.log");
twtl.Name = "text";
//Events of Warning level or up are written to log file
EventTypeFilter etf1 = new EventTypeFilter(SourceLevels.Warning);
twtl.Filter = etf1;
ts.Listeners.Add(twtl);
// set false to write to standard output stream
ConsoleTraceListener ctl = new ConsoleTraceListener(false);
ctl.Name = "console";
//Events of Information level or up are written to console
EventTypeFilter etf2 = new EventTypeFilter(SourceLevels.Information);
ctl.Filter = etf2;
ts.Listeners.Add(ctl);
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//To create an event source in Windows Vista, Windows XP Professional,
//or Windows Server 2003, you must have administrative privileges.
//MySource is the source used in code to reference the new event log.
//MyNewLog is the name shown in the Event View
//Run the following line only once and set a break point after it to allow enough time to create the event log
EventLog.CreateEventSource("MySource", "MyNewLog");
EventLogTraceListener evetl = new EventLogTraceListener("MySource");
evetl.Name = "eventLog";
//Events of Error level or up are written to event log
EventTypeFilter etf3 = new EventTypeFilter(SourceLevels.Error);
evetl.Filter = etf3;
ts.Listeners.Add(evetl);
//TraceEventType used here is first filtered by sw.Level
//to determine if the trace event should be sent to a listener.
//The TraceEventType has to be at the sw.Level or up to be sent.
//When a listener receives the event, it outputs only those events
//to which the listener's filter(s) matches. If no filter is defined
//on the filter, the listener outputs all events that send to it.
ts.TraceEvent(TraceEventType.Error, 1, "error message");
ts.TraceEvent(TraceEventType.Warning, 2, "warning message");
ts.TraceEvent(TraceEventType.Information, 3, "information message");
ts.Flush();
ts.Close();
}
----------
Part 2
---------
//using System;
//using System.Diagnostics;
private static void TraceTest()
{
TraceSource ts = new TraceSource("TestConsole");
ts.TraceEvent(TraceEventType.Error, 1, "error message");
ts.TraceEvent(TraceEventType.Warning, 2, "warning message");
ts.TraceEvent(TraceEventType.Information, 3, "information message");
ts.Flush();
ts.Close();
}
---- Code above uses the configuration settings below ---
<system.diagnostics>
<sharedListeners>
<add name="text" type="System.Diagnostics.TextWriterTraceListener" initializeData="testLog.log">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
</add>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" >
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Information" />
</add>
<add name="eventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="MyNewLog">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Error" />
</add>
</sharedListeners>
<sources>
<source name="TestConsole" switchName="testSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="text" />
<add name="console" />
<add name="eventLog" />
<remove name="Default"/>
</listeners>
</source>
</sources>
<switches>
<add name="testSwitch" value="Information"/>
</switches>
</system.diagnostics>
Thursday, November 26, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment