{"id":1768,"date":"2023-06-14T09:07:25","date_gmt":"2023-06-14T09:07:25","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1768"},"modified":"2023-06-14T09:07:25","modified_gmt":"2023-06-14T09:07:25","slug":"asp-net-core-logging-fundamentals","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/14\/asp-net-core-logging-fundamentals\/","title":{"rendered":"Asp.Net Core Logging Fundamentals"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1768\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1768\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<h1 class=\"wp-block-heading\">Fundamentals of Logging in .NET Core<\/h1>\n\n\n\n<p>.NET Core SDK is a light weight SDK which includes a bare minimum set of features required to build an application. We can install NuGet packages for other features we require for our application. For this, Microsoft provides .NET APIs as .NET Extensions.<\/p>\n\n\n\n<p>.NET Extensions is an open-source, cross-platform set of APIs for commonly used programming patterns and utilities, such as dependency injection, logging, and app configuration. Most APIs in this project are meant to work on many .NET platforms, such as .NET Core, .NET Framework, Xamarin, and other. While commonly used in ASP.NET Core applications, these APIs are not coupled to the ASP.NET Core application model. They can be used in console apps, WinForms and WPF, etc. You can find the documentation and the source code of extensions at&nbsp;<a href=\"https:\/\/github.com\/aspnet\/Extensions\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/aspnet\/Extensions<\/a>.<\/p>\n\n\n\n<p>All the extensions are included under the&nbsp;<code>Microsoft.Extensions<\/code>&nbsp;namespace. You can find all built-in and third party extensions at&nbsp;<a href=\"https:\/\/www.nuget.org\/packages?q=Microsoft.Extensions\" target=\"_blank\" rel=\"noreferrer noopener\">nuget.org\/packages<\/a>.<\/p>\n\n\n\n<p>The Logging API is included in the&nbsp;<code>Microsoft.Extensions.Logging<\/code>&nbsp;package. The Logging API does not work as standalone. It works with one or more logging providers that store or display logs to a particular medium such as Console, Debug, TraceListeners etc.<\/p>\n\n\n\n<p>So, there are two important building blocks for implementing logging in a .NET Core based application:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>Logging API<\/li>\n\n\n\n<li>Logging Providers<\/li>\n<\/ol>\n\n\n\n<p>The following figure illustrates logging in .NET Core:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"183\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture51.png\" alt=\"\" class=\"wp-image-1769\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture51.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture51-300x91.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/logging-dotnet-core.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.NET Frameworks<\/p>\n\n\n\n<p>As you can see in the above figure, the logging API in&nbsp;<code>Microsoft.Extensions.Logging<\/code>&nbsp;works on the .NET Core based applications whether it is ASP.NET Core or EF Core. You just need to use the logging API with one or more logging providers to implement logging in any .NET Core based application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Logging API<\/h2>\n\n\n\n<p>As mentioned before, Microsoft provides logging API as an extension in the wrapper&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.extensions.logging\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging<\/a>&nbsp;which comes as a NuGet package.<\/p>\n\n\n\n<p><code>Microsoft.Extensions.Logging<\/code>&nbsp;includes the necessary classes and interfaces for logging. The most important are the ILogger, ILoggerFactory, ILoggerProvider interfaces and the LoggerFactory class.<\/p>\n\n\n\n<p>The following figure shows the relationship between logging classes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"157\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture52.png\" alt=\"\" class=\"wp-image-1770\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture52.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture52-300x78.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/logging-api.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Logging API<\/p>\n\n\n\n<p>Let&#8217;s have an overview of each of the above class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>ILoggerFactory<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<code>ILoggerFactory<\/code>&nbsp;is the factory interface for creating an appropriate&nbsp;<code>ILogger<\/code>&nbsp;type instance and also for adding the&nbsp;<code>ILoggerProvider<\/code>&nbsp;instance.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>interface<code> <\/code>ILoggerFactory<code> : <\/code>IDisposable<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>ILogger<code> CreateLogger(<\/code>string<code> categoryName);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>void<code> AddProvider(<\/code>ILoggerProvider<code> provider);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The Logging API includes the built-in&nbsp;<code>LoggerFactory<\/code>&nbsp;class that implements the&nbsp;<code>ILoggerFactory<\/code>&nbsp;interface. We can use it to add an instance of type&nbsp;<code>ILoggerProvider<\/code>&nbsp;and to retrieve the&nbsp;<code>ILogger<\/code>&nbsp;instance for the specified category. Visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.extensions.logging.iloggerfactory\" target=\"_blank\" rel=\"noreferrer noopener\">ILoggerFactory<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.extensions.logging.loggerfactory\" target=\"_blank\" rel=\"noreferrer noopener\">LoggerFactory<\/a>&nbsp;for more information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>ILoggerProvider<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<code>ILoggerProvider<\/code>&nbsp;manages and creates an appropriate logger, specified by the logging category.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>interface<code> <\/code>ILoggerProvider<code> : <\/code>IDisposable<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>ILogger<code> CreateLogger(<\/code>string<code> categoryName);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>We can create our own logging provider by implementing the&nbsp;<code>ILoggerProvider<\/code>&nbsp;interface. Visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.extensions.logging.iloggerprovider\" target=\"_blank\" rel=\"noreferrer noopener\">ILoggerProvider<\/a>&nbsp;for more information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>ILogger<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<code>ILogger<\/code>&nbsp;interface includes methods for logging to the underlying storage. There are many extension methods which make logging easy. Visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.extensions.logging.ilogger\" target=\"_blank\" rel=\"noreferrer noopener\">ILogger<\/a>&nbsp;for more information.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>interface<code> <\/code>ILogger<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>void<code> Log&lt;<\/code>TState<code>&gt;(<\/code>LogLevel<code> logLevel, <\/code>EventId<code> eventId, <\/code>TState<code> state, <\/code>Exception<code> exception, <\/code>Func<code>&lt;TState, Exception, <\/code>string<code>&gt; formatter);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>bool<code> IsEnabled(<\/code>LogLevel<code> logLevel);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>IDisposable<code> BeginScope&lt;<\/code>TState<code>&gt;(<\/code>TState<code> state);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>} <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Logging Providers<\/h2>\n\n\n\n<p>A logging provider displays or stores logs to a particular medium such as a console, a debugging event, an event log, a trace listener, and others. Microsoft provides various logging providers as NuGet packages.<\/p>\n\n\n\n<p>The following table lists important logging providers.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Logging Provider&#8217;s NuGet Package<\/td><td>Output Target<\/td><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.Console\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.Console<\/a><\/td><td>Console<\/td><\/tr><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.AzureAppServices\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.AzureAppServices<\/a><\/td><td>Azure App Services &#8216;Diagnostics logs&#8217; and &#8216;Log stream&#8217; features<\/td><\/tr><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.Debug\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.Debug<\/a><\/td><td>Debugger Monitor<\/td><\/tr><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.EventLog\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.EventLog<\/a><\/td><td>Windows Event Log<\/td><\/tr><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.EventSource\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.EventSource<\/a><\/td><td>EventSource\/EventListener<\/td><\/tr><tr><td><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Logging.TraceSource\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft.Extensions.Logging.TraceSource<\/a><\/td><td>Trace Listener<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Microsoft has also collaborated with various logging framework teams (including third parties like NLog, Serilog, Loggr, Log4Net, and others) to extend the list of providers compatible with&nbsp;<code>Microsoft.Extensions.Logging<\/code>. The following are some thrid-party logging providers:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Logging Provider<\/td><td>Description<\/td><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/github.com\/elmahio\/Elmah.Io.Extensions.Logging\" target=\"_blank\" rel=\"noreferrer noopener\">elmah.io<\/a><\/td><td>Provider for the Elmah.Io service<\/td><\/tr><tr><td><a href=\"https:\/\/github.com\/imobile3\/Loggr.Extensions.Logging\" target=\"_blank\" rel=\"noreferrer noopener\">Loggr<\/a><\/td><td>Provider for the Logger service<\/td><\/tr><tr><td><a href=\"https:\/\/github.com\/NLog\/NLog.Extensions.Logging\" target=\"_blank\" rel=\"noreferrer noopener\">NLog<\/a><\/td><td>Provider for the NLog library<\/td><\/tr><tr><td><a href=\"https:\/\/github.com\/serilog\/serilog-framework-logging\" target=\"_blank\" rel=\"noreferrer noopener\">Serilog<\/a><\/td><td>Provider for the Serilog library<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Let&#8217;s take an example using the&nbsp;<code>Microsoft.Extensions.Logging.Console<\/code>&nbsp;package which displays logs on the Console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Console Logging Provider<\/h2>\n\n\n\n<p>Let&#8217;s see how to display logs on the console using the NuGet package for a console provider.<\/p>\n\n\n\n<p>The&nbsp;<code>Microsoft.Extensions.Logging.Console<\/code>&nbsp;package includes logging classes which send log output to the console.<\/p>\n\n\n\n<p>The following figure illustrates how the logging API works with the console logging provider.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/console-logger.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Logging API with Console Logging Provider<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"261\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture53.png\" alt=\"\" class=\"wp-image-1771\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture53.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture53-300x130.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>As you can see in the above figure, the&nbsp;<code>ConsoleLogger<\/code>&nbsp;implements&nbsp;<code>ILogger<\/code>, while the&nbsp;<code>ConsoleLoggingProvider<\/code>&nbsp;implements&nbsp;<code>ILoggingProvider<\/code>. The&nbsp;<code>ConsoleLoggerExtensions<\/code>&nbsp;class includes extension method&nbsp;<code>AddConsole()<\/code>, which adds a console logger to the&nbsp;<code>LoggerFactory<\/code>.<\/p>\n\n\n\n<p>Now, let&#8217;s see how to display logs on the Console in the .NET Core console application.<\/p>\n\n\n\n<p>First of all, create a new console application using the Console App (.NET Core) template in Visual Studio 2017 (or later).<\/p>\n\n\n\n<p>Now, you need to install a NuGet package of&nbsp;<code>Microsoft.Extensions.Logging<\/code>. You can install it either using the NuGet Package Manager or executing the following command in the Package Manager Console.<\/p>\n\n\n\n<p><samp>PM&gt; Install-Package Microsoft.Extensions.Logging<\/samp><\/p>\n\n\n\n<p>Now, you need to install a logging provider of your choice. Here, we will send logs on the Console, so, install the&nbsp;<code>Microsoft.Extensions.Logging.Console<\/code>&nbsp;package either using NPM or execute the following command in the Package Manager Console in Visual Studio.<\/p>\n\n\n\n<p><samp>PM&gt; Install-Package Microsoft.Extensions.Logging.Console<\/samp><\/p>\n\n\n\n<p>After successfully installing the above two packages, you can now implement logging in your .NET Core console application, as shown below.<\/p>\n\n\n\n<p>Example: Logging in .NET Core Console App<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace<code> DotnetCoreConsoleApp<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>class<code> <\/code>Program<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>static<code> <\/code>void<code> Main(<\/code>string<code>[] args)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>ILoggerFactory<code> loggerFactory = <\/code>new<code> <\/code>LoggerFactory<code>(<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>new<code>[] { <\/code>new<code> <\/code>ConsoleLoggerProvider<code>((_, __) =&gt; <\/code>true<code>, <\/code>true<code>) }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>\/\/or<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>\/\/ILoggerFactory loggerFactory = new LoggerFactory().AddConsole();<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code>ILogger<code> logger = loggerFactory.CreateLogger&lt;<\/code>Program<code>&gt;();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.LogInformation(<\/code>\"This is log message.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><samp>info: DotnetCoreConsoleApp.Program[0]<\/samp><br><samp>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This is log message.<\/samp><\/p>\n\n\n\n<p>In the above example, we created an object of the&nbsp;<code>LoggerFactory<\/code>&nbsp;class and assigned it to the&nbsp;<code>ILoggerFactory<\/code>&nbsp;type variable, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ILoggerFactory loggerFactory = new LoggerFactory(<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; new[] { new ConsoleLoggerProvider ((_, __) =&gt; true, true) }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">);<\/pre>\n\n\n\n<p>The&nbsp;<code>LoggerFactory<\/code>&nbsp;can contain one or more logging providers, which can be used to log to multiple mediums concurrently. The constructor of the&nbsp;<code>LoggerFactory<\/code>&nbsp;accepts an array of different logger provider objects as&nbsp;<code>new[] { }<\/code>. We want to display logs on the console, so we need to create an object of the console logger provider&nbsp;<code>ConsoleLoggerProvider<\/code>.<\/p>\n\n\n\n<p>There are four constructors of the&nbsp;<code>ConsoleLoggerProvider<\/code>. Use the one that allows lambda expression (Func&lt;&gt;) for log filtration and includeScope Boolean. Here, we don&#8217;t want to filter any information so the lambda expression would always return true&nbsp;<code>(_, __) =&gt; true<\/code>, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new ConsoleLoggerProvider((_, __) =&gt; true, true)<\/pre>\n\n\n\n<p>Then, we can use this object of the&nbsp;<code>LoggerFactory<\/code>&nbsp;to create a logger using which we can actually log information, errors, warnings, traces, debugs etc.&nbsp;<code>loggerFactory.CreateLogger&lt;Program&gt;()<\/code>&nbsp;creates a logger specific to the&nbsp;<code>Program<\/code>&nbsp;class so that the logger will display a message with context info, e.g. DotnetCoreConsoleApp.Program[0].<\/p>\n\n\n\n<p>Most logging providers include an extension method of&nbsp;<code>ILoggerFactory<\/code>, which is a shortcut to add a provider to the logger factory. For example, to add a console logger provider to the&nbsp;<code>LoggerFactory<\/code>, just call the&nbsp;<code>LoggerFactory.AddConsole()<\/code>&nbsp;extension method with the same parameters as&nbsp;<code>ConsoleLoggerProvider<\/code>, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> ILoggerFactory loggerFactory = <\/code>new<code> LoggerFactory().AddConsole();<\/code><\/pre>\n\n\n\n<p>This is more readable and maintainable than creating a logger provider manually. The above logger factory will display the same output as above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Log Levels<\/h2>\n\n\n\n<p>Log levels indicate the importance or severity of log messages. Built-in log providers include extension methods to indicate log levels.<\/p>\n\n\n\n<p>The following table lists log levels in .NET Core.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Log Level<\/td><td>Severity<\/td><td>Extension Method<\/td><td>Description<\/td><\/tr><\/thead><tbody><tr><td>Trace<\/td><td>0<\/td><td>LogTrace()<\/td><td>Logs messages only for tracing purposes for the developers.<\/td><\/tr><tr><td>Debug<\/td><td>1<\/td><td>LogDebug()<\/td><td>Logs messages for short-term debugging purposes.<\/td><\/tr><tr><td>Information<\/td><td>2<\/td><td>LogInformation()<\/td><td>Logs messages for the flow of the application.<\/td><\/tr><tr><td>Warning<\/td><td>3<\/td><td>LogWarning()<\/td><td>Logs messages for abnormal or unexpected events in the application flow.<\/td><\/tr><tr><td>Error<\/td><td>4<\/td><td>LogError()<\/td><td>Logs error messages.<\/td><\/tr><tr><td>Critical<\/td><td>5<\/td><td>LogCritical()<\/td><td>Logs failures messages that require immediate attention.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We can use extension methods to indicate the level of the log messages as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace<code> DotnetCoreConsoleApp<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>class<code> <\/code>Program<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>static<code> <\/code>void<code> Main(<\/code>string<code>[] args)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>ILoggerFactory<code> loggerFactory = new <\/code>LoggerFactory<code>().AddConsole((_, __) =&gt; <\/code>true<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>ILogger<code> logger = loggerFactory.CreateLogger&lt;<\/code>Program<code>&gt;();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.LogInformation(<\/code>\"Logging information.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.LogCritical(<\/code>\"Logging critical information.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.LogDebug(<\/code>\"Logging debug information.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.LogError(<\/code>\"Logging error information.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.LogTrace(<\/code>\"Logging trace\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.LogWarning(<\/code>\"Logging warning.\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The above example will display the following output:<\/p>\n\n\n\n<p>Logging in ASP.NET Core<\/p>\n\n\n\n<p>ASP.NET Core uses the same logging mechanism as .NET Core logging. So, it is highly recommended to go through the previous chapter&nbsp;<a href=\"https:\/\/www.tutorialsteacher.com\/core\/fundamentals-of-logging-in-dotnet-core\">Logging in .NET Core<\/a>&nbsp;before reading this.<\/p>\n\n\n\n<p>Here, we will implement logging in the ASP.NET Core 2.x MVC application.<\/p>\n\n\n\n<p>As explained in the previous chapter, the logging API in&nbsp;Microsoft.Extensions.Logging&nbsp;namespace works with one or more built-in or third party logging providers. So, in an ASP.NET Core MVC application, we will also have to install the NuGet package&nbsp;Microsoft.Extensions.Logging&nbsp;and one or more logging providers of our choice.<\/p>\n\n\n\n<p>Create an ASP.NET Core MVC application in Visual Studio 2017 (or later). When you create the ASP.NET Core MVC web application in Visual Studio 2017 (or later), it automatically includes the NuGet package for&nbsp;Microsoft.Extensions.Logging&nbsp;and the following logging providers under the&nbsp;Microsoft.AspNetCore.App&nbsp;NuGet package. So, we don&#8217;t have to install it manually.<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>Microsoft.Extensions.Logging.Console<\/li>\n\n\n\n<li>Microsoft.Extensions.Logging.Debug<\/li>\n\n\n\n<li>Microsoft.Extensions.Logging.EventSource<\/li>\n\n\n\n<li>Microsoft.Extensions.Logging.TraceSource<\/li>\n<\/ol>\n\n\n\n<p>Add Logging Providers<\/p>\n\n\n\n<p>As mentioned in the previous chapter, we need to add providers in&nbsp;LoggerFactory. In the ASP.NET Core MVC application, the call to the&nbsp;WebHost.CreateDefaultBuilder(args)&nbsp;method in the Program.cs internally adds the Console, Debug, and EventSource logging providers.<\/p>\n\n\n\n<p>Example: Program.cs<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>public class Program<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public static void Main(string[] args)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CreateWebHostBuilder(args).Build().Run();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebHost.CreateDefaultBuilder(args)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .UseStartup&lt;Startup&gt;();<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Look at the source code of the&nbsp;WebHost.CreateDefaultBuilder()&nbsp;method on&nbsp;<a href=\"https:\/\/github.com\/aspnet\/MetaPackages\/blob\/master\/src\/Microsoft.AspNetCore\/WebHost.cs\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a>&nbsp;and you will find the following code:<\/p>\n\n\n\n<p>.ConfigureLogging((hostingContext, logging) =&gt;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logging.AddConfiguration(hostingContext.Configuration.GetSection(&#8220;Logging&#8221;));<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logging.AddConsole();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logging.AddDebug();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logging.AddEventSourceLogger();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }).<\/p>\n\n\n\n<p>Thus, if you want to use these providers, no need to add them manually. If you want to use other providers or any default provider, then you need to remove all the existing providers and add the provider of your choice. To configure logging providers, call the&nbsp;ConfigureLogging()&nbsp;extension method of&nbsp;IWebHostBuilder, as shown below.<\/p>\n\n\n\n<p>Example: Program.cs<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; WebHost.CreateDefaultBuilder(args)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; .ConfigureLogging(logBuilder =&gt;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logBuilder.ClearProviders(); \/\/ removes all providers from LoggerFactory<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logBuilder.AddConsole();&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logBuilder.AddTraceSource(&#8220;Information, ActivityTracing&#8221;); \/\/ Add Trace listener provider<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; })<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; .UseStartup&lt;Startup&gt;();<\/p>\n\n\n\n<p>In the above example, the&nbsp;ConfigureLogging()&nbsp;method takes action to delegate&nbsp;Action&lt;ILogBuilder&gt;&nbsp;to configure logging providers. To add logging providers of your choice, remove all the default providers using&nbsp;ClearProviers()&nbsp;and then call the extension method of a provider to add it, such as&nbsp;AddTraceSource()&nbsp;which will add the trace listener provider, and the&nbsp;AddConsole()&nbsp;method which will add the Console logging provider.<\/p>\n\n\n\n<p>You can also configure the logging provider using&nbsp;ILoggerFactory&nbsp;in the&nbsp;Configure()&nbsp;method of the&nbsp;Startup&nbsp;class. Let&#8217;s see an example on how to store logs in a text file.<\/p>\n\n\n\n<p>Store Logs in a Text File<\/p>\n\n\n\n<p>To store logs in a file, install the NuGet package&nbsp;<a href=\"https:\/\/github.com\/serilog\/serilog-extensions-logging-file\" target=\"_blank\" rel=\"noreferrer noopener\">Serilog.Extensions.Logging.File<\/a>. Serillog includes an extension method for&nbsp;ILoggerFactory&nbsp;but not for&nbsp;ILogBuilder&nbsp;(in v 1.1.0). So, go to the&nbsp;Startup.cs&nbsp;file and add the&nbsp;ILoggerFactory&nbsp;parameter in the&nbsp;Configure()&nbsp;method. Then, call the&nbsp;AddFile()&nbsp;extension method to add Serillog file provider, as shown below. ASP.NET Core dependency injection will automatically pass an instance of the&nbsp;LoggerFactory&nbsp;for this parameter.<\/p>\n\n\n\n<p>Example: Startup.cs<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; \/\/ other code remove for clarity<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; loggerFactory.AddFile(&#8220;Logs\/mylog-{Date}.txt&#8221;);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>This will store all the logs in the mylog-&lt;date&gt;.txt file, under the Logs folder in your application.<\/p>\n\n\n\n<p>Create Logs in the Controller<\/p>\n\n\n\n<p>We can use&nbsp;ILogger&nbsp;or&nbsp;ILoggerFactory&nbsp;anywhere in an application using ASP.NET Core DI (Dependency Injection). Consider the following example of&nbsp;HomeController:<\/p>\n\n\n\n<p>Example: Logging in MVC Controller<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>namespace AspDotNetCoreMvcApp.Controllers<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public class HomeController : Controller<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private readonly ILogger _logger;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public HomeController(ILogger&lt;HomeController&gt; logger){<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger = logger;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IActionResult Index()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.LogInformation(&#8220;Log message in the Index() method&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return View();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IActionResult About()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.LogInformation(&#8220;Log message in the About() method&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return View();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>In the above example, the&nbsp;ILogger&lt;HomeController&gt;&nbsp;parameter is included in the constructor. ASP.NET Core DI will pass the&nbsp;ILogger&nbsp;instance, which can be used to log in the&nbsp;Index()&nbsp;and&nbsp;About()&nbsp;action methods.<\/p>\n\n\n\n<p>Passing&nbsp;HomeController&nbsp;as generic type for the&nbsp;ILogger&lt;HomeController&gt;, will be used as a category. For example, specifying&nbsp;ILogger&lt;HomeController&lt;&nbsp;will display a fully qualified name&nbsp;AspDotNetCoreMvcApp.Controllers.HomeController&nbsp;in the logs, as shown below.<\/p>\n\n\n\n<p>info: AspDoteNetCoreMvcApp.Controllers.HomeController[0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log message in the Index() method<\/p>\n\n\n\n<p>Let&#8217;s understand the above log message. Here, we logged information using the&nbsp;LogInformation()&nbsp;method, so it starts with &#8220;info:&#8221; followed by the fully qualified name of the class where a log is created:&nbsp;AspDoteNetCoreMvcApp.Controllers.HomeController[0]. [0] is the event id. You can specify this event id to identify a record, e.g. Id, page number or other important information which uniquely identifies a log. We didn&#8217;t specify any event id, so it will be 0. The next line is an actual log message: &#8220;Log message in the Index() method&#8221;.<\/p>\n\n\n\n<p>The same can be achieved by passing&nbsp;ILoggerFactory&nbsp;in the constructor.<\/p>\n\n\n\n<p>public class HomeController : Controller<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; private readonly ILogger _logger;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public HomeController(ILoggerFactory logFactory)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger = logFactory.CreateLogger&lt;HomeController&gt;();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public IActionResult Index()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.LogInformation(&#8220;Log message in the Index() method&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return View();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; public IActionResult About()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.LogInformation(&#8220;Log message in the About() method&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return View();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Now, run the above application from command prompt by navigating to&nbsp;\/&lt;app root folder&gt;\/bin\/debug\/netcoreapp2.1\/, run the&nbsp;dotnet &lt;app name&gt;.dll&nbsp;command and then open http:\/\/localhost:5000 in the browser. It will display the same logs on the Console as above.<\/p>\n\n\n\n<p>Logs:<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Route matched with {action = &#8220;Index&#8221;, controller = &#8220;Home&#8221;}. Executing acti<\/p>\n\n\n\n<p>on AspDoteNetCoreMvcApp.Controllers.HomeController.Index (AspDotNetCoreMvcApp)<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executing action method AspDoteNetCoreMvcApp.Controllers.HomeController.In<\/p>\n\n\n\n<p>dex (AspDotNetCoreMvcApp) &#8211; Validation state: Valid<\/p>\n\n\n\n<p><strong>info: AspDoteNetCoreMvcApp.Controllers.HomeController[0]<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log message in the Index() method<\/strong><\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executed action method AspDoteNetCoreMvcApp.Controllers.HomeController.Ind<\/p>\n\n\n\n<p>ex (AspDotNetCoreMvcApp), returned result Microsoft.AspNetCore.Mvc.ViewResult in<\/p>\n\n\n\n<p>&nbsp;0.8505ms.<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executing ViewResult, running view Index.<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[4]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executed ViewResult &#8211; view Index executed in 231.2839ms.<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executed action AspDoteNetCoreMvcApp.Controllers.HomeController.Index (Asp<\/p>\n\n\n\n<p>DotNetCoreMvcApp) in 288.6931ms<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 946.274ms 200 text\/html; charset=utf-8<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/images\/banner1.svg<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/images\/banner2.svg<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 5.6471ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 6.5811ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/css\/site.min.css<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.2811ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/js\/site.min.js<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/images\/banner3.svg<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.178ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.2342ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/css\/site.min.css<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.1173ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/js\/site.min.js<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.2539ms 404<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request starting HTTP\/1.1 GET http:\/\/localhost:5000\/favicon.ico<\/p>\n\n\n\n<p>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request finished in 0.3253ms 404<\/p>\n\n\n\n<p>Thus, we can implement logging in ASP.NET Core MVC application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fundamentals of Logging in .NET Core .NET Core SDK is a light weight SDK which includes a bare minimum set of features required to build an application. We can install NuGet packages for other features we require for our application. For this, Microsoft provides .NET APIs as .NET Extensions. .NET Extensions is an open-source, cross-platform [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[36,35],"class_list":["post-1768","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","tag-asp-net","tag-fundamental"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":22,"uagb_excerpt":"Fundamentals of Logging in .NET Core .NET Core SDK is a light weight SDK which includes a bare minimum set of features required to build an application. We can install NuGet packages for other features we require for our application. For this, Microsoft provides .NET APIs as .NET Extensions. .NET Extensions is an open-source, cross-platform&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1768"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1768\/revisions"}],"predecessor-version":[{"id":1772,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1768\/revisions\/1772"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1768"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}