ASP.NET Core – Overview of of built-in logging


ASP.NET built in logging

In the previous articles we have seen some brand new features in ASP.NET Core. In this article I will explain the built-in logging capability of ASP.NET Core. Built-in logging capability reduces the effort to implement logging functionality .

What is new in ASP.NET core logging

In previous version of ASP.NET we were creating an own logging framework or using other third party components like loge4net. This logging component will be tightly coupled with the application. The ASP.NET core has nice brand new logging mechanism with dependency injection . The one of the coolest thing is that the logging functionality is enabled by default . You can see the logs in command prompt or in PoweShell using DNX command . DNX provides the hosting mechanism for Windows , Linux and Mac in ASP.NET Core.

Implement logging in ASP.NET Core

In the current ASP.NET core version, we have two options to implement logging. You either use ILogger via dependency injection or using ILoggerFactory . In the startup.cs class we have configure method with ILoggerFactory parameter and we can add whatever output we want for logging. The AddConsole() method is responsible to add the logs in the console by default. You can see the configure method in the following screen.

ASP.NET Core - Overview of built-in logging

Open the PowerShell or command prompt and set the directory path to your ASP.NET core application path and enter the command dnx web . You can see the our application is running on port 5000 . Enter the localhost URL in the browser (localhost:5000). Navigate to some random pages.

ASP.NET Core - Overview of built-in logging - Run application on power shell
ASP.NET Core - Overview of built-in logging - Application running

Look at the PowerShell windows, there you can see all the logs from your application .

ASP.NET Core - Overview of built-in logging - Logs in powershell

Logging via dependency injection (ILogger)

Your application can request an instance of ILogger as a dependency in a class’s constructor. By requesting an instance of ILogger, your class doesn’t need to create an instance of a logger via ILoggerFactory. To log with ILogger, Create the constructor of the class as follows. In the Index action you can see the LogInformation method to log the information .

 private ILogger<HomeController> _logger;
        public HomeController (ILogger<HomeController> logger)
            _logger = logger;
        public IActionResult Index()
            _logger.LogInformation("Heyy.. home page requested");
            return View();

Run the application and navigate to the home page, then you can see the logs in the PowerShell.

ASP.NET Core - Overview of built-in logging - Custom logging

Different Log levels

We have different type of log levels to control the verbosity of the logging output from your application. These log levels helps us to differentiate the log messages to different loggers.

ASP.NET Core - Overview of built-in logging - logs types

Look at the loglevel class and enumerator definitions in GitHub .

  • Debug – Logs that contain the most detailed messages. These messages may contain sensitive application data.These messages are disabled by default and should never be enabled in a production environment
  • Verbose – Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.
  • Information – Logs that track the general flow of the application. These logs should have long-term value.
  • Warning – Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.
  • Errors – Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.
  • Critical – Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention
  • None – Not used for writing log messages. Specifies that a logging category should not write any messages.
  • The following two tabs change content below.

    Tom Mohan

    Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS

    Latest posts by Tom Mohan (see all)

    • Nice Article!. Do you know how to use it SQL Server. I want to log all this log to SQL Server how I can do that?