Tomas Aschan
Tomas Aschan

Reputation: 60564

How do I log a general exception to the Event Log?

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?

Upvotes: 1

Views: 227

Answers (1)

RickyKaare
RickyKaare

Reputation: 38

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.

Upvotes: 1

Related Questions