This is how I implemented event logging. I created a common ILogger interface, so I can swap different registration mechanisms:
interface ILogger { void Debug(string text); void Warn(string text); void Error(string text); void Error(string text, Exception ex); }
My implementation class is very simple:
class EventLogger : ILogger { public void Debug(string text) { EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Information); } public void Warn(string text) { EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Warning); } public void Error(string text) { EventLog.WriteEntry("MyAppName", text, EventLogEntryType.Error); } public void Error(string text, Exception ex) { Error(text); Error(ex.StackTrace); } }
Please note that I am not creating an EventLog. To use my logger class, I just have the following link (you could return this static factory method):
private static readonly ILogger log = new EventLogger();
And the actual use looks like this:
try { // business logic } catch (Exception ex) { log.Error("Exception in MyMethodName()", ex); }
Nelson Jul 15 '09 at 19:28 2009-07-15 19:28
source share