Click here to Skip to main content
Click here to Skip to main content
Go to top

Writing to System Event Log

, 3 Sep 2008
Rate this:
Please Sign up or sign in to vote.
How to write to System Event Log from a C# application

Introduction

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

Background

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)
{
    try
    {
        string FilePath, Line;
        FilePath = "C:\\MySampleFile.txt";
        StreamReader m_StreamReader;
        m_StreamReader = File.OpenText(FilePath);
        while ((Line = m_StreamReader.ReadLine()) != null)
        {
            MessageBox.Show(Line);        
        }
        m_StreamReader.Close();
    }
    catch (Exception ex)
    {
        EventLog m_EventLog = new EventLog("");
        m_EventLog.Source = "MySampleEventLog";
        m_EventLog.WriteEntry("Reading text file failed " + ex.Message,
            EventLogEntryType.FailureAudit);

        MessageBox.Show(
           "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.

1.gif

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.

2.gif

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.

History

  • 3rd September, 2008: Initial post

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionIt helped PinmemberMember 85535542-Jul-13 3:11 
QuestionSource Code for Reference Pinmembermohanjune198723-Oct-12 20:13 
Questionhow do i get the error information from the system event log PinmemberRocky_Bas24-Sep-12 16:19 
GeneralMy vote of 4 PinmemberDaveMolo6-Jul-12 0:10 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by Hari Narayanan R.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid