Click here to Skip to main content
15,173,985 members
Articles / Desktop Programming / Win32
Posted 3 Sep 2008

Tagged as


21 bookmarked

Writing to System Event Log

Rate me:
Please Sign up or sign in to vote.
4.36/5 (9 votes)
3 Sep 2008CPOL3 min read
How to write to System Event Log from a C# application


This article provides details on how to write to the System Event log from a C# application.


In many cases, it's best not only to detect and catch errors, but to log them as well. For example, some problems may occur only when your application is dealing with a particularly large load. Other problems may occur, with no obvious reasons as expected, only in certain remote situations. To diagnose these errors and build a larger picture of the application problems, you need to log errors automatically so they can be reviewed at a later time.

The .NET Framework provides a wide range of logging options. When certain errors occur, you can send emails, add a database record or write to a file. The following section throws light on how you can log error details to the System Event Log from your .NET application.

The Event Log

The Windows event log basically maintains three types of logs:

  • Application: Used to track errors or notifications from any application
  • Security: Used to track security related problems, used by the OS
  • System: Used to track OS events

Each event record identifies the source (generally the application or service that created the record), the type of notification (error, information, warning) and the time it was logged. You can double-click on the log to view additional information such as a text description.

What to Log?

One of the potential problems with event logs is that they are automatically overwritten when the maximum set size is reached or after a certain number of days (typically seven days). This means application logs cannot be used to log critical information that need to be retained for a long period of time. Instead, they should be used to track information that is valuable only for a short period of time. For example, the event logs can be used to review errors and diagnose strange behavior immediately after it occurs, say within a couple of days or so.

Using the Code

The following sample code snippet shows how to write to the System Event Log. Here we try to read the contents of a text file. If the specified text file exists in the specified path, the contents are displayed. If the file is not found, control flow is passed to the catch() block and the corresponding error is logged to the system event log.

private void btnEventLogger_Click(object sender, EventArgs e)
        string FilePath, Line;
        FilePath = "C:\\MySampleFile.txt";
        StreamReader m_StreamReader;
        m_StreamReader = File.OpenText(FilePath);
        while ((Line = m_StreamReader.ReadLine()) != null)
    catch (Exception ex)
        EventLog m_EventLog = new EventLog("");
        m_EventLog.Source = "MySampleEventLog";
        m_EventLog.WriteEntry("Reading text file failed " + ex.Message,

           "Successfully wrote the following to the System Event Log : \n" 
			+ ex.Message); 

Sample Event Log Images

The Event Viewer can be accessed from Administrative Tools > Event Viewer.


On double clicking an item from the list, the details of that item would be opened up. The error message can be viewed in the Description area.


Points of Interest

The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters. Therefore, include this namespace to use the EventLog class.

Event logging uses disk space and normally takes processor time away from applications. So do not store unimportant information or large quantities of data in the event log. Generally, an event log should be used to log unexpected conditions or errors and not user actions or performance tracking information.


  • 3rd September, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

QuestionIt helped Pin
Buddhi Madarasinghe2-Jul-13 4:11
MemberBuddhi Madarasinghe2-Jul-13 4:11 
QuestionSource Code for Reference Pin
mohanjune198723-Oct-12 21:13
Membermohanjune198723-Oct-12 21:13 
Questionhow do i get the error information from the system event log Pin
Rocky_Bas24-Sep-12 17:19
MemberRocky_Bas24-Sep-12 17:19 

Programatically using MFC ,how do i get the error information from the event log.
GeneralMy vote of 4 Pin
DaveMolo6-Jul-12 1:10
MemberDaveMolo6-Jul-12 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.