Logging enhancement for backend projects using aspnet or dotnet generic host
by providing common infrastructure and sensible defaults.
Focus on writing and designing business-logic
and less time worrying about operational concerns.
First of all this package is already included in the Intility templates, but can be installed separately. Installation is done with the dotnet add package
command or via Visual Studio Package Manager.
# install common infrastructure
dotnet add package Intility.Logging.AspNetCore
To instrument the runtime with the new logging capabilities you will need to use an extension method on the IHostBuilder
interface
// Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseIntilityLogging((hostContext, logging) =>
{
// add default metadata on log events
logging.UseDefaultEnrichers();
})
//...
The base package inclues a Console sink with a format supporting structured logging. Use configuration section Serilog
to configure the loglevel override and other supported settings. See Serilog for more information.
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"System": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Properties": {
"Application": "MyApp"
}
}
}
Additional sinks can be installed separately if needed. Simply register the new sinks to the logging builder after package installation is complete.
# install Elasticsearch sink
dotnet add package Intility.Extensions.Logging.Elasticsearch
# install Sentry sink
dotnet add package Intility.Extensions.Logging.Sentry
// Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseIntilityLogging((hostContext, logging) =>
{
logging.UseDefaultEnrichers()
.UseElasticsearch()
.UseSentry();
})
//...
{
"Elasticsearch": {
"Endpoints": "localhost:9200",
"IndexFormat": "my-service-{0:yyyy.MM}"
},
"Sentry": {
"Dsn": "https://examplePublicKey@o0.ingest.sentry.io/0",
"MaxRequestBodySize": "Always",
"SendDefaultPii": true,
"MinimumBreadcrumbLevel": "Debug",
"MinimumEventLevel": "Warning",
"AttachStackTrace": true,
"Debug": true,
"DiagnosticsLevel": "Error"
}
}