Branch | Build status |
---|---|
master | |
develop |
Adds the logging capability, by default uses Serilog for logging with 3 optional extensions (sinks):
- Console
- File
- Seq
dotnet add package Joint.Logging
- Joint
- Microsoft.Extensions.Logging
- Serilog
- Serilog.AspNetCore
- Serilog.Extensions.Logging
- Serilog.Sinks.Console
- Serilog.Sinks.ElasticSearch
- Serilog.Sinks.File
- Serilog.Sinks.Seq
Extend Program.cs
-> CreateDefaultBuilder()
with UseLogging()
that will add the required services and configure ILogger
available in ASP.NET Core framework.
public static IWebHostBuilder GetWebHostBuilder(string[] args)
=> WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddJoint().Build())
.UseLogging();
Then, simply inject ILogger<T>
(being ASP.NET Core built-in abstraction) to write the logs.
public class SomeService
{
private readonly ILogger<SomeService> _logger;
public SomeService(ILogger<SomeService> logger)
{
_logger = logger;
}
public void Foo()
{
_logger.LogInformation("Foo");
}
}
- applicationName - sets the optional application name property used for log enrichment.
- serviceId - sets the optional service id property used for log enrichment.
- excludePaths - optional endpoints that should be excluded from logging (e.g. while performing the health checks by other services).
- console.enabled - enables/disables console logger.
- file.enabled - enables/disables file logger.
- file.path - path to the file logs.
- file.interval - how often should the new file with logs be created.
- seq.enabled - enables/disables Seq logger.
- seq.url - URL to Seq API.
- seq.token - API key (if provided) used while sending logs to Seq.
"logger": {
"applicationName": "some-service",
"serviceId": "instance-1",
"excludePaths": ["/ping", "/metrics"],
"console": {
"enabled": true
},
"file": {
"enabled": true,
"path": "logs/logs.txt",
"interval": "day"
},
"seq": {
"enabled": true,
"url": "http://localhost:5341",
"token": "secret"
}
}