views:

19

answers:

1

I have a windows service, in which I want a top level try-catch that catches any otherwise unhandled (or bubbled) exception, logs it to the Event Log and then swallows it so the service keeps running. However, I can't find any overload to System.Diagnostics.EventLog.WriteEntry that takes an exception as a parameter - is there no way to just give the event log the exception and let it parse out the message on its own?

A: 

Unfortunately there is no standard way of just passing the Exception to the Eventlog, built in to the .NET framework.

To have an exception written to the EventLog with the smallest development effort, you would need to write something like:

EventLog myLog = new EventLog();
myLog.Source = "Your Source";
myLog.WriteEntry(exception.ToString(), EventLogEntryType.Error);

But normally you would try to do some formatting of your exception.

RickyRocks
OK - that's what I suspected was true, but hoped wasn't. Thanks anyway!
Tomas Lycken