How to write to an event log by using Visual C# (307024)



The information in this article applies to:

  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)

This article was previously published under Q307024
For a Microsoft Visual C++ .NET version of this article, see 815661.
For a Microsoft Visual Basic .NET version of this article, see 301279.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Diagnostics

IN THIS TASK

SUMMARY

This step-by-step article shows you how to add your own entries to the operating system's event log by using the Microsoft .NET Framework.

back to the top

Requirements

The following list describes the recommended hardware, software, network infrastructure, and service packs that you will need:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
  • Microsoft Visual Studio .NET or Visual Studio 2005
back to the top

Write to an Event Log

Event logging provides a standard, centralized way for your applications to record important software and hardware events. Windows supplies a standard user interface for viewing the logs, the Event Viewer. By using the common language's run-time EventLog component, you can connect to existing event logs easily, on both local and remote computers, and write entries to these logs. You can also read entries from existing logs and create your own custom event logs. In its simplest form, writing to an event log involves only a few steps to create a sample application. To do this, follow these steps:
  1. Open Visual Studio .NET or Visual Studio 2005.
  2. Create a new Console Application in Microsoft Visual C# .NET or Visual C# 2005 creates a public class and an empty Main method for you.
  3. Verify that the project references at least the System.dll.
  4. Use the using directive on the System and System.Diagnostics namespaces so that you do not have to qualify declarations from these namespaces later in your code. You must use these statements before any other declarations.
    using System;
    using System.Diagnostics;
    					
  5. To write to an event log, you must have several pieces of information: Your message, the name of the log you to which you want to write (which will be created if it does not already exist), and a string that represents the source of the event. You can register a particular source with only a single event log; if you want to write messages to more than one log, you must define multiple sources.
    string sSource;
    string sLog;
    string sEvent;
    
    sSource = "dotNET Sample App";
    sLog = "Application";
    sEvent = "Sample Event";
    					
  6. Use two static methods of the EventLog class to check whether your source exists, and then, if the source does not exist, to create this source that is associated with a particular event log. If the log name that you specify does not exist, the name is created automatically when you write your first entry to the log. By default, if you do not supply a log name to the CreateEventSource method, the log file is named Application Log.
    if (!EventLog.SourceExists(sSource))
    	EventLog.CreateEventSource(sSource,sLog);
    					
  7. To write a message to an event log, you can use the static method EventLog.WriteEntry, which has several different overloaded versions. The following sample code shows the simplest method (which takes a source string and your message) and one of the more complex methods (which supports specifying the event ID and event type):
    EventLog.WriteEntry(sSource,sEvent);
    EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning,  234);
    					
  8. Save your application. Run your application, and then check the Application log in the Event Viewer to see your new events.
back to the top

Complete Code Listing

using System;
using System.Diagnostics;

namespace WriteToAnEventLog_csharp
{
	/// Summary description for Class1.
	class Class1
	{
		static void Main(string[] args)
		{
			string sSource;
			string sLog;
			string sEvent;

			sSource = "dotNET Sample App";
			sLog = "Application";
			sEvent = "Sample Event";

			if (!EventLog.SourceExists(sSource))
				EventLog.CreateEventSource(sSource,sLog);

			EventLog.WriteEntry(sSource,sEvent);
			EventLog.WriteEntry(sSource, sEvent,
				EventLogEntryType.Warning, 234);
		}
	}
}
				


back to the top

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbDebug kbHOWTOmaster KB307024 kbAudDeveloper