Explain how logging works in ASP.NET Core


Logging is the process of recording events in software as they happen in real-time, along with other information such as the infrastructure details, time taken to execute, etc. Logging is an essential part of any software application. Having logs is crucial, especially when things go wrong. Logs help you understand the failures or performance bottlenecks and fix the problems.

Logs are typically written to a database, console, or file, depending on the severity of the application and convenience. Although it's possible to record any data in the logs, you write informational messages and error messages in general. The informational messages capture standard events as they happen, e.g. a method call, user authentication, product checkout, etc. The error messages capture the errors and provide all the data that might help you debug the program.

ASP.NET Core provides a generic logging interface to make it easy to log stuff from your application. The same interface is used throughout the framework and the third-party libraries, making it easier to search through the logs and diagnose the problem. The framework also allows you to configure the verbosity of your logs and send the logs to one or more destinations like file, console, or database.

In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).

You can override the default set of logging providers by calling ClearProviders and adding the specific logging providers that you need. The following code adds the console logging provider.

public static IHostBuilder CreateHostBuilder(string[] args) =>
   Host.CreateDefaultBuilder(args)
      .ConfigureLogging(logging =>{
         logging.ClearProviders();
         logging.AddConsole();
      })
      .ConfigureWebHostDefaults(webBuilder =>{
         webBuilder.UseStartup<Startup>();
      });

You can create logs using the ILogger interface, which is injected into your code by the ASP.NET Core dependency injection container. The following example illustrates this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace Razor.Pages{
   public class IndexModel : PageModel{
      private readonly ILogger<IndexModel> _logger;
      public IndexModel(ILogger<IndexModel> logger){
         _logger = logger;
      }

      public void OnGet(){
         _logger.LogInformation("Index page visited.");
      }
   }
}

Logging is essential for quickly diagnosing, troubleshooting, and fixing errors in a production application. It's a best practice to configure logging as soon as possible, ideally when you start developing your application.

Updated on: 22-Jun-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements