K
Ken
I would like to start using EventLogTraceListener, and am running into
a couple of significant limitations:
1) I have found that there is no way to write EventLog entries with
different EventLogEntryTypes. It seems that EventLogTraceListener is
only capable of writing entries with EventLogEventType.Information,
even for Trace.Fail. Is this assessment correct?
2) EventLogTraceListener.Flush (and the idea of buffering writes) is
not applicable to EventLogTraceListener. It seems that each call to
Trace.Write() or Trace.WriteLine() results in the creation of a new
EventLog entry.
This behavior is undesirable and incompatible with my usage of
Trace.Write() and Trace.WriteLine(). For example:
try
{
// Remove the DefaultTraceListener
Trace.Listeners.Clear();
// Create EventLogTraceListener with an EventSource of "Test"
EventLogTraceListener evListener = new EventLogTraceListener("Test");
// Register the listener
Trace.Listeners.Add(evListener);
// Raise an exception
throw new Exception("Fake exception");
}
catch(Exception ex)
{
// Construct trace output message with date and exception info
Trace.Write(System.DateTime.Now.ToString());
Trace.Write(ex.GetType().Name);
Trace.Write(ex.Message);
Trace.Write("\r\n");
Trace.WriteLine(ex.StackTrace);
}
The code above uses Write() and WriteLine() in the way that I believe
they were intended, based on the presence of other formatting methods
for intentation. This use of Trace works great for file logging.
If I add the EventLogTraceListener, however, the above code
will create five entries in the EventViewer, no matter how I use
Trace.AutoFlush and Trace.Flush(). This means that
EventLogTraceListener is essentially unusable for me if I have
widespread usage of Trace like that above.
Subclassing TraceListener to create a listener that writes "correctly"
to the EventViewer is not a viable option because the Trace class has
no members that would help to group a series of Write()/WriteLine()
calls into a a single logical log entry.
So ultimately, it is not possible to use Trace's
Write()/WriteLine()/buffering capabilities (standard for logging to
files and console) AND create meaningful EventViewer entries. This
seems like an oversight in the design of the Trace API.
Please comment; I would gladly be proven wrong on either point,
especially #2.
Ken
a couple of significant limitations:
1) I have found that there is no way to write EventLog entries with
different EventLogEntryTypes. It seems that EventLogTraceListener is
only capable of writing entries with EventLogEventType.Information,
even for Trace.Fail. Is this assessment correct?
2) EventLogTraceListener.Flush (and the idea of buffering writes) is
not applicable to EventLogTraceListener. It seems that each call to
Trace.Write() or Trace.WriteLine() results in the creation of a new
EventLog entry.
This behavior is undesirable and incompatible with my usage of
Trace.Write() and Trace.WriteLine(). For example:
try
{
// Remove the DefaultTraceListener
Trace.Listeners.Clear();
// Create EventLogTraceListener with an EventSource of "Test"
EventLogTraceListener evListener = new EventLogTraceListener("Test");
// Register the listener
Trace.Listeners.Add(evListener);
// Raise an exception
throw new Exception("Fake exception");
}
catch(Exception ex)
{
// Construct trace output message with date and exception info
Trace.Write(System.DateTime.Now.ToString());
Trace.Write(ex.GetType().Name);
Trace.Write(ex.Message);
Trace.Write("\r\n");
Trace.WriteLine(ex.StackTrace);
}
The code above uses Write() and WriteLine() in the way that I believe
they were intended, based on the presence of other formatting methods
for intentation. This use of Trace works great for file logging.
If I add the EventLogTraceListener, however, the above code
will create five entries in the EventViewer, no matter how I use
Trace.AutoFlush and Trace.Flush(). This means that
EventLogTraceListener is essentially unusable for me if I have
widespread usage of Trace like that above.
Subclassing TraceListener to create a listener that writes "correctly"
to the EventViewer is not a viable option because the Trace class has
no members that would help to group a series of Write()/WriteLine()
calls into a a single logical log entry.
So ultimately, it is not possible to use Trace's
Write()/WriteLine()/buffering capabilities (standard for logging to
files and console) AND create meaningful EventViewer entries. This
seems like an oversight in the design of the Trace API.
Please comment; I would gladly be proven wrong on either point,
especially #2.
Ken