Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Logging in ASP.NET Core

Suggested Videos
Part 58 - Centralised 404 error handling in ASP.NET Core | Text | Slides
Part 59 - UseStatusCodePagesWithRedirects vs UseStatusCodePagesWithReExecute | Text | Slides
Part 60 - Global exception handling in core mvc | Text | Slides

In this video we will discuss Logging in ASP.NET Core.

Default Logging in ASP.NET Core

We can run an core application from the Command Line or from Visual Studio. 

To run core project from the command line
  1. Launch the command prompt as an administrator
  2. Change the path to the folder that contains your project 
  3. Finally execute the following dot net run command
  4. c:\Projects\EmployeeManagement\EmployeeManagement>dotnet run

When we run the project from the command line using the dotnet run command we see lot of information logged to the console as shown below.

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Route matched with {action = "Index", controller = "Home"}. Executing acti
on EmployeeManagement.Controllers.HomeController.Index (EmployeeManagement)
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method EmployeeManagement.Controllers.HomeController.Inde
x (EmployeeManagement) - Validation state: Valid
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action method EmployeeManagement.Controllers.HomeController.Index
 (EmployeeManagement), returned result Microsoft.AspNetCore.Mvc.ViewResult in 0.
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[1]
      Executing ViewResult, running view Index.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.2.0-rtm-35687 initialized 'AppDbContext' using pro
vider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MaxPoolSize=128
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (32ms) [Parameters=[], CommandType='Text', CommandTimeo
      SELECT [e].[Id], [e].[Department], [e].[Email], [e].[Name], [e].[PhotoPath
      FROM [Employees] AS [e]
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[4]
      Executed ViewResult - view Index executed in 4763.7879ms.

We see a similar output in the Debug window if we run the project from Visual Studio. To open the Debug window, 
  1. Click on the Debug menu in Visual Studio and select "Windows" and "Output"
  2. In the Output window, select Debug from the Show output from dropdownlist
Logging providers in ASP.NET Core

A logging provider is the component that stores or displays logs. For example, the Console log provider displays logs on the console. Similarly, the Debug log provider displays logs on the Debug window in Visual Studio.

ASP.NET Core built-in logging providers
  • Console
  • Debug
  • EventSource
  • EventLog
  • TraceSource
  • AzureAppServicesFile
  • AzureAppServicesBlob
  • ApplicationInsights
Third party logging providers for ASP.NET Core
  • NLog
  • elmah
  • Serilog
  • Sentry
  • Gelf
  • JSNLog
  • Loggr
  • Stackdriver
Default logging providers in ASP.NET Core Main() method in the Program class in Program.cs file is the entry point for an core application. This method calls CreateDefaultBuilder() method performs several tasks like 
  • Setting up the web server 
  • Loading the host and application configuration from various configuration sources and 
  • Configuring logging
Since ASP.NET Core is open source we can see the complete source on their official github page. The following is the code snippet from CreateDefaultBuilder() method. 

.ConfigureLogging((hostingContext, logging) =>

As part of configuring logging, CreateDefaultBuilder() method adds the following 3 logging providers by default. This is the reason when we run the core project we see the log displayed both on the console and on the debug window in Visual Studio.
  1. Console
  2. Debug
  3. EventSource
CreateDefaultBuilder() method looks for Logging section in the application configuration file appsettings.json.

Logging section in appsettings.json

The following is the Logging section in appsettings.json file on my machine.

"Logging": {
  "LogLevel": {
    "Default": "Warning",
    "Microsoft":  "Warning"

LogLevel is used to control how much log data is logged or displayed. We will discuss Log level in detail in our upcoming videos.

To turn off iisexpress.exe logs under debug tab in visual studio

disable iisexpress.exe logs under debug tab in visual studio

In Visual Studio click on Tools - Options. On the Options window, under Debugging - Output Window turn off the messages you do not need.

visual studio debug tab turn off isexpress.exe messages core tutorial for beginners

No comments:

Post a Comment

It would be great if you can help share these free resources