Skip to content

peereflits/Shared.Logging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

Peereflits.Shared.Logging

Peereflits.Shared.Logging uses Serilog as logging engine and can be used in .NET projects from .NET Core 3.1 onwards. The purpose of this package is to apply the (configuration of) logging for all projects in an unambiguous way.

The logging can write logs to three different Serilog "Sinks" (target systems):

  1. Console
  2. Logz.io
  3. Application Insights

When is running in "Development" mode if defaults to Console and aborts other configured sinks.

Peereflits.Shared.Logging consists of three packages where "Shared" is not used separately:

  1. Logging.WebApps+Logging.Shared is for (standard) ASP.NET (core) applications;
  2. Logging.Functions+Logging.Shared is for use within Azure Functions.

Logging.Shared contains the logging setup & configuration used by Logging.WebApps and Logging.Functions.

Logging Configuration

  1. For console logging set the enviroment variable Logging:LogTo:Console to 1.
  2. For logging to Logz.io set the enviroment variable Logging:LogTo:Logzio to 1 and provide valid values for environment variables Logging:Logzio:Token and Logging:Logzio:DataCenter:SubDomain.
  3. For logging to Application Insights set the enviroment variable Logging:LogTo:ApplicationInsights to 1 and configure Application Insights properly in the Azure WebApp (including APPINSIGHTS_INSTRUMENTATIONKEY and APPLICATIONINSIGHTS_CONNECTION_STRING). It will then log to the AppInsights "Traces".

The default loglevel is LogEventLevel.Information. This can be adjusted by providing the environment variable Logging:LogLevel with one of the following values:

  • Fatal or critical: LogEventLevel.Fatal
  • Error: LogEventLevel.Error
  • Warning: LogEventLevel.Warning
  • Information: LogEventLevel.Information
  • Debug: LogEventLevel.Debug
  • Trace of verbose: LogEventLevel.Verbose

Add logging in ASP'NET (core)

The recommended way to apply Serilog is via the IHostBuilder extension methods. The extension method UseSerilog in HostBuilderExtensions from the Peereflits.Shared.Logging.WebApps package can be used for this. The example below uses the default settings:

using Peereflits.Shared.Logging;

public class program
{
     public static void Main(string[] args)
     {
         CreateHostBuilder(args).Build().Run();
     }

     private static IHostBuilder CreateHostBuilder(string[] args)
     => return Host.CreateDefaultBuilder(args)
                   .UseSerilog()
                   .ConfigureWebHostDefaults(builder => { builder.UseStartup<Startup>(); });
}

The example below uses the default application configuration. After reading the application configuration, the various enrichers and sinks are added to the configuration that is set up by default.

For more information see: Serilog.Settings.Configuration

     Host.CreateDefaultBuilder(args)
         .UseSerilog(useApplicationConfiguration: true)
         .ConfigureWebHostDefaults(builder => { builder.UseStartup<Startup>(); });

Note: If a sink is added twice, a log will also be written twice.

Request logging

Serilog can also provide (web-)request logging. To add these, you can use the extension method UseRequestLogging in the class ApplicationBuilderExtensions. This adds one log line per request containing all relevant information, including time. The duration is relevant for measuring the performance of the APIs and can be found in the field Elapsed. In addition, it automatically adds the user agent of the user in the field RequestUserAgent.

Adding the request logging middleware should be done as early as possible when configuring this middleware. Only requests handled by handlers added after this middleware are logged. Middleware that handles requests that explicitly should not be logged, such as static files, should stand for this again.

For example:

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    
    // Middleware die niet gelogd dient te worden zoals static files
    app.UseStaticFiles();
    
    app.UseSerilogRequestLogging();
    // Andere app configuratie en middleware
    ...

It is not possible to use request logging if only the different overloads of IServiceCollection.AddSerilog are used in this class.

Add logging in Azure Functions

The recommended way to apply logging is via the IFunctionsHostBuilder extension methods. The extension method UseSerilog in FunctionsHostBuilderExtensions from the Peereflits.Shared.Logging.Functions package can be used for this. See the example below:

[assembly: FunctionsStartup(typeof(Startup))]

public class Startup : FunctionsStartup
{
   public override void Configure(IFunctionsHostBuilder builder)
   {
       builder.UseSerilog("My.Application.Name")
               .AddSingleton(...)
               ;
        ...
    }
}

In UseSerilog the (logical) name of the function must be given as a parameter. The value of this parameter is found in the logging as ApplicationName.

Version support

The libraries supports the following .NET versions:

  1. .NET 6.0
  2. .NET 7.0
  3. .NET 8.0

© No copyright applicable
® "Peereflits" is my codename.


About

An extension library for logging

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages