Application diagnostics
.NET Core C# Dependency Injection Diagnostics Patterns Visual Studio

Adding Logging Capability in a .NET Core Application

Welcome to today’s post.

In today’s post I will look at how logging capability can be added to your .NET Core Web Applications.

Logging can be configured is in Startup.cs and it is quite straightforward. To be able to use logging, the following namespace will need to be added in the using section of your source file:

using Microsoft.Extensions.Logging;

Next, the Configure() start up method will need the ILogger parameter as shown below:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
{
   if (env.IsDevelopment())
   {
	..
   }
   else
   {
       	..
   }
	..

To enable logging for different types of context use the logging service middleware to enable it within ConfigureServices() in startup.cs:

services.AddLogging(loggingBuilder => loggingBuilder
                .AddConsole()
                .AddDebug());

To call the logger and output some text, run a command like the one below:

logger.LogInformation("Application starting in development mode.");

When the application is run, the log output will be shown in the output window of your IDE as shown:

If your logging does not output to the debugger or standard console then check your appSettings.json for correctly configured logging files:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

If you specify the default log level to “Warning” or “Error” and output an information message, then the output will not show as it does not match the filter.

To configure services to use logging, you can use constructor injection as shown:

ILogger _logger;
IBookService _bookService;

public SearchController(ApplicationDbContext db, IBookService bookService, ILogger<BookController> logger)
{
   _db = db;
   _logger = logger;
   _bookService = bookService;
}

Note: If you attempt to use the ILogger constructor parameter as shown:

ILogger logger

Then you will end up with the following error:

An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'BookLoan.Controllers.SearchController'.
Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

The reason for this is that each ILogger instance needs to be created with an instance of a type T, which is a category type.

The category type for constructor injection corresponds to the container class.

During debugging of the ILogger instance, you can see the category type corresponds to the type of the class T that the logger is injected into as shown:

When output, the log will show the fully qualified category type name as shown and the output message:

For more details refer to the .NET core documentation at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1.

That’s all for today’s post.

I hope you found it informative and useful.

Social media & sharing icons powered by UltimatelySocial