The Windows operating system logs data into the Windows Event Log whenever a problem occurs. You can view this data using the Windows Event Viewer tool. This article discusses how you can programmatically work with the Windows Event Log in C#.
To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can .
Create a .NET Core console application project in Visual Studio
First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.
- Launch the Visual Studio IDE.
- Click on “Create new project.”
- In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
- Click Next.
- In the “Configure your new project” window shown next, specify the name and location for the new project.
- Click Create.
This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project to work with the Windows event log in the subsequent sections of this article.
Install the EventLog NuGet package
To be able to work with the Windows Event Log in .NET Core applications, you should install the Microsoft.Extensions.Logging.EventLog package from NuGet. You can do this either via the NuGet Package Manager inside the Visual Studio 2019 IDE, or by executing the following command at the NuGet Package Manager Console:
Create an instance of the EventLog class in C#
To create an instance of the EventLog class and write an entry to the Windows Event Log, you can use the following code:
The Windows Event Log is typically used to record system events, network traffic, and related data such as security, performance, etc. You can take advantage of the Windows Event Log as a log target to store your application’s data. If your application runs only on Windows, the Windows Event Log is a nice option for storing your application’s event log data.
How to do more in C#:
Copyright © 2020 IDG Communications, Inc.