We all need to know what is happening in our application when they are running. Sometimes that is in the IDE we are using and sometimes that means storing it in a computer logs or maybe a database. In .NET Core 2.2, there are new ways to log messages so let’s take a look at how to do that with Visual Studio and Rider.

Setting Up Logging

We will be demoing on an ASP.NET Core 2.2 MVC project. This all starts with installing the Microsoft.Extensions.Logging NuGet package in our project. The package will allow us to perform the default logging for our web app. There are other NuGet packages that expand on the NuGet package but for now, we will just look the basics.

We need to modify the Program class of the project to configure logging. Below is the code that will clear the logs we will be using, add the configuration details from appSettings.json for logging, allow logging to the console, also adds logging details to the debug window and finally adds logging details to the local computer’s event log.

Before we start with the details of Loggin configuration, we need to look at the levels of logging at our disposal. We have six levels of logging data as described.

  • Tracing: Tracing purpose messages
  • Debug: Short term debugging purposes
  • Information: Track the flow of the application
  • Warning: Abnormal program flow
  • Error: Errors and exceptions that can’t be handled by your code
  • Critical: Serious system failures or errors

Setting up the logging levels in the appSettings.json brings custom details for our applications. In our Logging section outlined below, we can see that we can set the minimum level of detail for each log type that we set up for our application. In this example, we have configured Debug logging to only log Information level and above while Console logging has been set up for minimum logging depending on the namespace that the logging occurs. The last section of LogLevel gives the default level of logging for the application.

Logging in our ASP.NET app

Once we have logging configured at the start of the application, we can begin to log details in our code. We will look at an example in an ASP.NET Core MVC project specifically in the HomeController. The ILogger has been added to the Dependency Injection (DI) container and allows us to call ILogger<T> to have an ILogger instance created based on the fully qualified type given for T. In this case, the type will be the HomeController.

With the ILogger<HomeController> created, we can add our messages to the logs and with the needed levels by calling certain methods on the logger:

  • LogTrace()
  • LogDebug()
  • LogInformation()
  • LogWarning()
  • LogError()
  • LogCritical()

We have set up an Information level log message for the Index and Privacy Actions on the Home Controller. For those routes for the Home Controller that cannot be mapped, we have an Error action that will present the user with an Error view. In this case, we will also log an Error level message.

If we take a look at Visual Studio 2017 and Rider 2018.3, we can see that the Debug messages are logged and presented in the Output Window (VS) and Run Tool Window (Rider) respectfully.

We can also see that the Error log level messages are also added to each IDE’s log details.

As we have seen, .NET Core 2.2 allows developers to bring logging details for our projects and solutions with ease using the built-in Dependency Injection. This blog post I hope has brought some insight into this well-changed portion of .NET Core.

I highly recommend that you try Rider for your .NET development! You can download Rider 2018.3 and give it a try!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.