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):
- Console
- Logz.io
- 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:
Logging.WebApps+Logging.Sharedis for (standard) ASP.NET (core) applications;- Logging.Functions+Logging.Shared is for use within Azure Functions.
Logging.Shared contains the logging setup & configuration used by Logging.WebApps and Logging.Functions.
- For console logging set the enviroment variable
Logging:LogTo:Consoleto 1. - For logging to Logz.io set the enviroment variable
Logging:LogTo:Logzioto 1 and provide valid values for environment variablesLogging:Logzio:TokenandLogging:Logzio:DataCenter:SubDomain. - For logging to Application Insights set the enviroment variable
Logging:LogTo:ApplicationInsightsto 1 and configure Application Insights properly in the Azure WebApp (includingAPPINSIGHTS_INSTRUMENTATIONKEYandAPPLICATIONINSIGHTS_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
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.
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.
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.
The libraries supports the following .NET versions:
- .NET 6.0
- .NET 7.0
- .NET 8.0
© No copyright applicable
® "Peereflits" is my codename.