Home
Manage Your Code
Snippet: Write to the event log (C#)
Title: Write to the event log Language: C#
Description: This will write to the event log. Needs the using System.Diagnostics; statement. Views: 1031
Author: Charley McCollum Date Added: 3/10/2008
Copy Code  
1 {
2                string sSource;
3                string sLog;
4                string sEvent;
5
6                sSource = "C# Test Application";
7                sLog = "Application";
8                sEvent = "Message";
9
10                if (!EventLog.SourceExists(sSource))
11                    EventLog.CreateEventSource(sSource, sLog);
12
13                //EventLog.WriteEntry(sSource, sEvent);
14                //EventLog.WriteEntry(sSource, sEvent,EventLogEntryType.Warning, 234);
15                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 078);
16            }
Usage
Can write to any of the logs by changing the slog variable. 
Can be a message a warning or an error by changing "eventlogentrytype.warning".
The message goes into the sEvent variable. The Application name goes into the sSource var.
The log(Application, Security, etc..) goes into the sLog.