Microsoft & .NET.NETInteracting with the Windows 2000 Event Viewer Using C#

Interacting with the Windows 2000 Event Viewer Using C#

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Consider the need for a software program developed using C# language to interact with Microsoft Windows 2000’s built-in utility, Event Viewer. Imagine, developing a critical client-server application. You may need to provide a capability to access Windows 2000’s built-in utilities like Event Viewer using the software you are creating.

Interestingly, the .NET Framework provides a wide range of namespaces and class libraries for developing programs with such advanced capabilities. Even though, many languages provide facility for performing these types of advanced operations, .NET Framework provides much more simplified approach in your quest for building robust applications. Consider the need from two angles: From a user’s perspective and from an administrator’s.

Users: It’s possible to keep track of all kinds of errors and events occurring to the system from the C# application itself. It will be available in the form of human readable logs. Users can read these log entries with a click of a button, without knowing that event viewer utility has been accessed. A user can read the exact time the problem or an event occurred.

Admin: An administrator can write messages to these logs through a GUI front-end application developed using C# or any .NET language. It’s also possible for an administrator to read all of the messages on the client’s system. Remedial measures can be taken based on these log entries. An admin can also pass messages to clients and the same can be displayed in message boxes on the relevant client machine. The entries can also be displayed in text boxes. Moreover, these features can be changed as per the requirements. For example, you can give write permission to users as well.

In this way, users and administrators can directly interact with the Windows 2000 Operating System utilities through user-friendly applications. As a primary requirement, you should have a system with Windows 2000 and .NET Framework SDK installed on it. A text editor like Notepad is sufficient but you can also use other editors like Antechinus C# Editor. These types of editors can be downloaded from the Internet easily and it will also support features like color coding, syntax highlighting etc. 

Windows 2000 ships with a useful tool called Event Viewer, more often known as a Microsoft Management Console (MMC) Snap-in. It can be located under Start | Programs | Administrative Tools | Event Viewer. With the help of this viewer, you can monitor information about software and hardware installed on your computer and also identify any system problems and errors. 

Figure 1.1. Event Viewer.

For example, if your C# program generated an exception (meaning runtime error), the same will be recorded or logged as an entry under the Application Log of the Viewer. The corresponding event type will be Information. If an error occurs, the event type will be Error. In this article, you will learn how to read from and write to the Event Viewer logs programmatically using C# language.

Writing to the Event Viewer Log

You can write to the viewer using the EventLog class under System.Diagnostics namespace of the .NET Framework. This class provides a lot of properties like Source, Log, Close, and WriteEntrywith which you can manipulate the Event Viewer. Listing 1.1 given below illustrates how to create and write a event log upon clicking a button:

Listing 1.1:

//Source File : Eventwrite.cs
//Compilation : csc Eventwrite.cs
//Execution   : Eventwrite
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

public class Eventwrite: Form 
{
Button b1 = new Button();

public Eventwrite() 
{
this.Text = "An Article for Developer.com by Anand";
b1.Text = "Click here";
b1.Click += new EventHandler(b1_click);
b1.Location = new Point(100,50);
this.Controls.Add(b1);
}

public void b1_click(object sender, EventArgs e) 
{
//An object of the EventLog class created
EventLog elog = new EventLog();
elog.Log = "Application";
elog.Source = "From Developer.com article";
elog.WriteEntry("Hello, I'm from C#");
elog.Close();
MessageBox.Show("One message successfully written to EventViewer",
                "Anand.N");
}

public static void Main() 
{
Application.Run(new Eventwrite());
}

}

Upon execution of the above program, the output looks like as in Figure 1.2

Figure 1.2 – Output of Eventwrite.cs.

After clicking the button, launch Event Viewer and you will be able to view a log entry "From Developer.comarticle" below the
Source
heading. Right click the entry and select properties from the pop up menu. Now you will be able to view the message "Hello, I’m from C#" on the Description box of the Information Properties dialog (Figure 1.3).

Figure 1.3 – Event Viewer Properties.

Reading from the Event Viewer Log

You can read information from the viewer using the same class as mentioned above; however, you have to iterate a variable in a for loop. The variable specifies how many entries you want to read from the viewer. Listing 1.2 examines reading of five Event Viewer logs. All entries are displayed on message boxes one after another.

Listing 1.2:

//Source File : Eventread.cs
//Compilation : csc Eventread.cs
//Execution   : Eventread

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

public class Eventread: Form 
{
  Button b1 = new Button();

  public Eventread() {
    this.Text = "An Article for Developer.com by Anand";
    b1.Text = "Click here";
    b1.Click += new EventHandler(b1_click);
    b1.Location = new Point(100,50);
    this.Controls.Add(b1);
  }

  public void b1_click(object sender, EventArgs e) 
  {
    EventLog elog = new EventLog();
    elog.Log = "Application";
    elog.Source = "From Developer.com article";

    for(int i = 0; i<5;i++) 
    {

    try 
    {

      MessageBox.Show("Message: " +elog.Entries[i].Message + "n" +
                      "App: " +elog.Entries[i].Source + "n" +
                      "Entry type: " +elog.Entries[i].EntryType);
    }catch{}

    }

  }

  public static void Main() 
  {
    Application.Run(new Eventread());
  }

}

Entries is one of the properties of the EventLog class. This property returns an instance of EventLog.EventLogEntryCollection, which defines a number of EventLogEntry types, corresponding to relevant entries in a Event Log. In the above example, I have applied three of those types with which you can read log entries. Other types includes UserName, TimeGenrated, TimeWritten etc. Just substitute these types in the above listing and observe the output.


Try – Yourself Questions

  1. MMC stands for __________
  2. __________ namespace is used for manipulating the Event Viewer. 
  3. __________ class is used for reading from the Event Viewer. 
  4. How will you locate the Event Viewer in Windows 2000. 
  5. Entries is one of the properties of the __________ class. 

Download

You can download the source codes used in this article by clicking here

About the Author

Anand Narayanaswamy works as a freelance Web developer and freelance writer. He lives in Thiruvananthapuram, Kerala State, India. He runs learnxpress.com and provides free technical support to users worldwide through the site besides featuring tutorials and articles related to Java, C#, Visual Basic, and other web technologies.

# # #

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories