Event Logs in .NET Core Worker Application

Introduction

Logging is essential for software development practices, but it’s often underestimated until the program crashes. Logging serves numerous purposes including root cause analysis, bug analysis, and even performance reviews for the application.

There are great many libraries and approaches about how to incorporate Logging in your application. But sometimes we just need very basic level of logging and bringing in a big framework for this requirement might be an over kill.

In this post, we will see how to start with simple event logging in a .NET Core Worker application.

Setting the Scene

I started by creating a .NET Core Worker Service project as shown below:

A worker service is a .NET project built using a template which supplies a few useful features that turn a regular console application into something more powerful. A worker service runs on top of the concept of a host, which maintains the lifetime of the application. The host also makes available some familiar features, such as dependency injection, logging and configuration.

Worker services will generally be long-running services, performing some regularly occurring workload.

The default template gives us a very good starting point to demonstrate the logging code:

If you run the project, you’ll see console logging in action:

Let’s see how we can log this information to windows event logs.

Add Nuget Package for Event Logging Provider

First, lets add corresponding nuget package as shown below:

Configure Event Logging Middleware

After the nuget package is installed, we can configure the logging middleware in Program.cs file as shown below:

Update logging configuration in appsettings.json file:

If you run the application now, you’ll notice that logging information still did not show up in windows event viewer. The reason is that, currently, the log-level warning or above will be shown in the event logs by default, but we can change it to loglevel info by changing the appsettings.json file as shown below:

Inspecting the Event Logs in Event Viewer

Lets run the application for couple of minutes and then start event viewer in windows and we can see that application logs are being recorded there:

However, it is totally possible that you do not see any event logs regarding application. If this the case, run visual studio as administrator and run the application for couple of minutes again and check again the event viewer and you shall see that logs are available there.

Summary

Logging plays very important role in developing applications. In this post, we learned how to setup event logging in .NET Core application and the necessary configurations.

The demo application source code can be downloaded from this git repository.

If you are requirements are bit advanced and you need more structured logging approach, I covered those in another article you can read.

Let me know if you have some comments or questions. Till next time, Happy Coding.

My Recent Books