Use this package to integrate Serilog into your ASP.Net Web API and to logs to Grafana Loki.
Notes:
1 - To view logs in console with color themes you need to run the project with HTTP or HTTPS Profile.
2 - This example is a very simple one straightforward to the point. You can add more settings to Serilog through appsettings.json
3 - Loki Connection String can be defined in appsettings.json or on the configure method as a lambda function
Follow these steps to get it done:
dotnet add package JF91.SerilogWithLoki
SerilogExtensions.CreateLogger(builder.Configuration);
builder.Host.ConfigureSerilog();
builder.Host.ConfigureSerilog(() => "LokiConnection");
// OR
builder.Host.ConfigureSerilog(() =>
{
// Your code
return "LokiConnection";
});
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug"
}
}
"SerilogSettings": {
"SerilogSinks": {
"Console": true,
"Loki": true
},
"LokiSettings": {
"Url": "http://localhost:3100",
"CustomLabels": [
"Label1",
"Label2"
]
}
}
"APPLICATION_NAME": "MyKickassApi"
public MyClass(ILogger<MyClass> logger)
{
_logger = logger;
}
public void MyMethod()
{
_logger.LogDebug("Debug Log");
_logger.LogTrace("Trace Log");
_logger.LogCritical("Critical Log");
_logger.LogError("Error Log");
_logger.LogWarning("Warning Log");
_logger.LogInformation("Information Log");
}