This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

Logging configuration in .NET core

552 views 0 comments last modified about 12 months ago Raymond

.net core

.NET core introduces a logging API that works with a number of logging frameworks. The built-in providers are configurable and extensible to support different level loggings.

Built-in Logging Providers

Microsoft provides a number of built-in providers.  There are two logging providers created in the project templates: Console and Debug.

Console writes logs to the console while Debug uses System.Diagnostics.Debug to write logs.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDatabaseInitializer dbInitializer)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

//…

}

There are also other built-in providers like EventSource, EventLog, Azure App Service and TraceSource.

Logging Configurations

In the above example, it specifies Logging section in configuration file is configured for Console logging provider.

"Logging": {
     "IncludeScopes": false,
     "LogLevel": {
       "Default": "Warning"
     }
   }

About Scope

IncludeScopes indicates whether logging scope is supported.

Scope can be used to group a number of logical operations to attach the same data to each log created within that scope.

For more details, please refer to:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x#log-scopes

LogLevel

Different levels of logging are supported. As described in the following code, the most detailed level is Trace. In the configuration sample ‘"Default": "Warning"’, default logging level is Warning, which means only Warning, Error and Critical messages will be logged in Console.

namespace Microsoft.Extensions.Logging
{
     //
     // Summary:
     //     Defines logging severity levels.
     public enum LogLevel
     {
         //
         // Summary:
         //     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.
         Trace = 0,
         //
         // Summary:
         //     Logs that are used for interactive investigation during development. These logs
         //     should primarily contain information useful for debugging and have no long-term
         //     value.
         Debug = 1,
         //
         // Summary:
         //     Logs that track the general flow of the application. These logs should have long-term
         //     value.
         Information = 2,
         //
         // Summary:
         //     Logs that highlight an abnormal or unexpected event in the application flow,
         //     but do not otherwise cause the application execution to stop.
         Warning = 3,
         //
         // Summary:
         //     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.
         Error = 4,
         //
         // Summary:
         //     Logs that describe an unrecoverable application or system crash, or a catastrophic
         //     failure that requires immediate attention.
         Critical = 5,
         //
         // Summary:
         //     Not used for writing log messages. Specifies that a logging category should not
         //     write any messages.
         None = 6
     }
}

Another Example

{
     "Logging": {
         "LogLevel": {
             "MyController": "Error",

            "MyNamespace": "Debug",
             "Default": "Information",
             "Microsoft": "Warning"       
         }
     }
}

In this example above, the log levels are:

  1. Controller MyController: Error
  2. All the categories that begin with MyNamespace: Debug
  3. Default log level is Information (will be used when no filters match)
  4. Microsoft: only Warning messages will be logged.

Configure using Code

You can also use code in your Startup class to configure loggings and filters.

services.AddLogging(options =>
             {
                 options.AddFilter("Microsoft", LogLevel.Critical);
             });

Related pages

Set AttachDbFilename as Relative Path in .NET Core

145 views   0 comments last modified about 3 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

31 views   0 comments last modified about 3 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

SQLite in .NET Core with Entity Framework Core

84 views   0 comments last modified about 3 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

48 views   0 comments last modified about 3 months ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

Tuples in C# (4.x, 7.0, 7.1)

161 views   0 comments last modified about 7 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

308 views   0 comments last modified about 7 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.