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 asp.net 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 asp.net core application from the Command Line or from Visual Studio.
To run asp.net core project from the command line
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.
We see a similar output in the Debug window if we run the project from Visual Studio. To open the Debug window,
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
As part of configuring logging, CreateDefaultBuilder() method adds the following 3 logging providers by default. This is the reason when we run the asp.net core project we see the log displayed both on the console and on the debug window in Visual Studio.
Logging section in appsettings.json
The following is the Logging section in appsettings.json file on my machine.
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
In Visual Studio click on Tools - Options. On the Options window, under Debugging - Output Window turn off the messages you do not need.
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 asp.net 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 asp.net core application from the Command Line or from Visual Studio.
To run asp.net core project from the command line
- Launch the command prompt as an administrator
- Change the path to the folder that contains your project
- Finally execute the following dot net run command
- 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.
8168ms.
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
ut='30']
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.
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.
8168ms.
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
ut='30']
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,
- Click on the Debug menu in Visual Studio and select "Windows" and "Output"
- In the Output window, select Debug from the Show output from dropdownlist
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
- NLog
- elmah
- Serilog
- Sentry
- Gelf
- JSNLog
- KissLog.net
- Loggr
- Stackdriver
- Setting up the web server
- Loading the host and application configuration from various configuration sources and
- Configuring logging
.ConfigureLogging((hostingContext,
logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
As part of configuring logging, CreateDefaultBuilder() method adds the following 3 logging providers by default. This is the reason when we run the asp.net core project we see the log displayed both on the console and on the debug window in Visual Studio.
- Console
- Debug
- EventSource
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

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


No comments:
Post a Comment
It would be great if you can help share these free resources