How to Write to the Event Log Application in C#

Luqman Khan Feb 02, 2024
  1. Write to the Event Log Application in C#
  2. Methods to Write Entries to the Event Log Application in C#
How to Write to the Event Log Application in C#

The Windows event log is a complete record of the system, security, and application notifications stored by the Windows operating system. Administrators use it to detect and diagnose system problems and warn about future issues.

Write to the Event Log Application in C#

First, we will see that we can write to the event log. For this purpose, we can use the following code:

Syntax:

using (EventLog neweventLog = new EventLog("Application"))
{
    neweventLog.Source = "Application";
    neweventLog.WriteEntry("event Log message example", EventLogEntryType.Information, 102, 1);
}

Now, let us explain all the parts of the above syntax.

  1. EventLog: The Windows event log is used for logging your application’s errors or major events because administrators can easily access it. Windows Event Logs can be managed from a similar console.
  2. neweventLog.Source: In the above event source in the eventlog key, each log contains subkeys called event source; in other words, the name of the software which logs the event is called event source. It can be the name of the application or the name of the subcomponent of the application.

Methods to Write Entries to the Event Log Application in C#

For writing event log entries, you can use the following methods of the EventLog class:

  1. WriteEntry() method
  2. WriteEvent() method

the WriteEntry() Method

WriteEntry() requires a source parameter in the static method. The source can also be assigned with the constructor of the EventLog class.

The first is the log name, then the local machine and the event source name are defined in the constructor.

There are three event log entries which are written in the following:

  1. message: This is the first parameter of the WriteEntry() method.
  2. EventLogEntryType: You can define the event log entry with EventLogEntryType. There are different icons in the Event Viewer, depending on the type: Information, Warning, Error for Auditing SuecessAudit, and FailureAudit.
  3. event ID: We can specify an application-specific event ID that the application can use. We can also pass application-specific binary data and a category in addition.

You do not always need to create a new event source to write. You can write it with an existing one with the same name as the event log.

Let us see how we can programmatically work with the Windows event log in C#. We can create a source in the code below if it does not exist.

Example code:

private string CreateEventSource(string mynewAppName) {
  string eventSource = mynewAppName;
  bool sourceExists;
  try {
    sourceExists = EventLog.SourceExists(eventSource);
    if (!sourceExists) {
      EventLog.CreateEventSource(eventSource, "Application");
    }
  } catch (SecurityException) {
    eventSource = "Application";
  }

  return eventSource;
}

Now, let us explain the parts of the example code above.

  1. sourceExists: With this searching, the source throws a security exception only when it does not exist.
  2. mynewAppName: You can call the source by mynewAppName.

In case of an error, the user needs admin permission for security. It is pretty simple; you only have to use the .NET framework System.Diagnostics.EventLog class.

First, we will make an instance of the System.Diagnostics.EventLog class in the WriteEntry() method. Then, we will use the EventLog.SourceExists method to check whether the event source system exists.

We will create it under the System EventLog if it does not exist. Our logged events will appear under the System EventLog, and the event source will show system.

Example code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace neweventlogg {
  internal class Program {
    static void Main(string[] args) {
      using (EventLog neweventLog = new EventLog("Application")) {
        neweventLog.Source = "Application";
        neweventLog.WriteEntry("event Log message example", EventLogEntryType.Information, 102, 1);
      }
    }
  }
}

A class logger is created in the above code; then, the EventLog is made. If the EventLog already exists, the string message will be stored; otherwise, a new event source will be created.

By using the WriteEvent() and WriteEntry() methods, we can write events to an event log. But in some cases, you will need special permission to write to an event log application due to security reasons.

Related Article - Csharp Event