Posts

Showing posts with the label Application Log

How To Write to the Event Log in C#

I've been doing a lot of C# developing for Microsoft Exchange lately and I would like to share with you a few lines of C# that I've found to be very useful in event handling. I prefer using the windows event logs and not text files for logging since they will not grow out of control and are very easy to search and handle. The function below will create an event in the specified log, under the specified source with the provided information: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private static void WriteEvent (String LogName, String EventSource, String EventBody, int EventId, EventLogEntryType EventType) { try { using (EventLog eventLog = new EventLog(LogName)) { if (!EventLog.SourceExists(EventSource)) EventLog.CreateEventSource(EventSource, LogName); eventLog.Source = EventSource; eventLog.W