Last update:
August 26, 2024
Extend Logging¶
The Platform reads Serilog configuration from the appsettings.json file and can also be configured from the code. To achieve this, Platform provides ILoggerConfigurationService
interface that allows users writing and adding their own sinks or making customizations.
appsettings.json
UseSerilog((context, services, loggerConfiguration) =>
{
// read from configuration
_ = loggerConfiguration.ReadFrom.Configuration(context.Configuration);
// enrich configuration from external sources
var configurationServices = services.GetService<IEnumerable<ILoggerConfigurationService>>();
foreach (var service in configurationServices)
{
service.Configure(loggerConfiguration);
}
})
This code shows how Serilog is being initialized in the Platform:
-
First the
loggerConfiguration
objects are initialized from the configuration sections and then passed to a list of external services. To implement your own config services, first create a class that inheritsILoggerConfig
and implement it. For example, this is the implementation for the Azure Application Insights sink:appsettings.jsonpublic class ApplicationInsightsLoggerConfiguration : ILoggerConfigurationService { private readonly TelemetryConfiguration _configuration; public ApplicationInsightsLoggerConfiguration(TelemetryConfiguration configuration) { _configuration = configuration; } public void Configure(LoggerConfiguration loggerConfiguration) { loggerConfiguration.WriteTo.ApplicationInsights(telemetryConfiguration: _configuration, telemetryConverter: TelemetryConverter.Traces, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error); } }
-
Register the implementation in module.cs Initialize method of your external module: