Write Event Logs
Complete code list
Summary
This article demonstrates how to use the Microsoft. NET Framework to add your own entries to the event logs of the operating system.
Requirements
The following table summarizes the recommended hardware, software, network architecture, and required Service Packs: • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
• Microsoft Visual Studio. NET
Write Event Logs
Event Logs provide applications with a standard and centralized method to record important software and hardware events. Windows provides a standard user interface for viewing logs, that is, the event viewer. Using the EventLog component of the Runtime Library in the common language, you can easily connect to existing Event Logs on local and remote computers and enter entries in these logs. You can also read entries from existing logs and create your own Custom Event Logs. In the simplest way, you can create a sample application in just a few steps and write it into the event log: 1. Open Visual Studio. NET
2. Create a console application in Microsoft C. Visual C #. NET creates a public class and an empty Main () method for you.
3. Make sure that the project references at least System. dll.
4. Use the using command for the System and System. Diagnostics namespaces. In this way, you do not need to restrict the declarations in these namespaces in the subsequent code. These statements must be placed before all other statements.
Using System;
Using System. Diagnostics;
5. to write Event Logs, you need to provide the following information: your message, the name of the log to be written (if it does not exist, a name will be created), and a string that represents the event source. A source can only be recorded in one event log. Therefore, to write messages to multiple logs, you must define multiple sources.
String sSource;
String sLog;
String sEvent;
SSource = "dotNET Sample App ";
SLog = "Application ";
SEvent = "Sample Event ";
6. with all this information, the first step is to use two static methods of the EventLog class to check whether your event source exists. If not, create an event source associated with the specific event log. If the specified log name does not exist, the name is automatically created when the first entry is written. If no log name is provided for the CreateEventSource method, application logs are specified by default.
If (! EventLog. SourceExists (sSource ))
EventLog. CreateEventSource (sSource, sLog );
7. To write messages to Event Logs, you must use the static EventLog. WriteEntry method (there are multiple overloaded versions ). The following code provides the simplest method. You only need to provide the source string and your message, and the more complex method also requires the event ID and event type.
EventLog. WriteEntry (sSource, sEvent );
EventLog. WriteEntry (sSource, sEvent, EventLogEntryType. Warning, 234 );
8. Save and run the code, and then check the application logs in the event viewer to view your new events.
Complete code list
Using System;
Using System. Diagnostics;
Namespace WriteToAnEventLog_csharp
{
/// <Summary>
/// Summary description for Class1.
/// </Summary>
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 );
}
}
}
<BR/>
<BR/>