Meh, that previous post was kind of too simple etc, so thought I’d wrap up the logic in a nice class here you go.
It ccouldn’t be easier to call one example to write exceptions is
[codesyntax lang=”csharp”]
EventLogger.Write(ex.Message, “error”);
[/codesyntax]
The name of the assembly is captured by reflection to identify it from other applications in the event log.
[codesyntax lang=”csharp”]
using System.Diagnostics; using System.Reflection; namespace utilities { public static class EventLogger { /// <summary> /// Write entry to application event log /// </summary> /// <param name="message">Text to write to log</param> /// <param name="type">"information" / "error" / "success" defaults to information (optional)</param> public static void Write(string message, string type = "information") { var applicationName = Assembly.GetExecutingAssembly().GetName().Name; var sType = EventLogEntryType.Information; switch (type) { case "error": sType = EventLogEntryType.Error; break; case "success": sType = EventLogEntryType.SuccessAudit; break; } if (!EventLog.SourceExists(applicationName)) EventLog.CreateEventSource(applicationName, "Application"); EventLog.WriteEntry(applicationName, message, sType, 0); } } }
[/codesyntax]