There is little documentation on logger settings through the configuration file. Can anyone help - just an example of using the settings of the logger. In the example - all logs are written in one sample.txt. Logs on a call of a certain API / api/health - in a separate file and are not included in sample.txt. And an example ad - IMyLogger-writes to a separate SampleMy.txt. You can add many sections, and divide the logs by different criteria. It is better to set local logging levels as minimal, they will be overwritten by the global level. The global filter will exclude logs from all sub-loggers (I don't use it). PS sorry for the bad English)
"Serilog": {
"MinimumLevel": "Information", //<- global error level. Ovveride all local error level
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"MinimumLevel": "Debug", // <- local error level.
//Only records with Information logging level will be written to the log file
//but if ovveride global level to Debug, and dont override local error level -> it will still be global
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log\\SampleHealthCheck-.txt", //write health-check log in different file
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [CorrId:{CorrelationId}] [Op:{OperationId}] [U:{UserName}] {Message:lj}{NewLine}{Exception}"
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "RequestPath like '%/api/health'"
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log\\SampleMy-.txt", //Write some log in different file. Control through code
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [CorrId:{CorrelationId}] [Op:{OperationId}] [U:{UserName}] {Message:lj}{NewLine}{Exception}"
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "SourceContext = 'MyProject.IMyLogger'"
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log\\Sample-.txt", //all logs, without health-check
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [CorrId:{CorrelationId}] [Op:{OperationId}] [U:{UserName}] {Message:lj}{NewLine}{Exception}"
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "RequestPath like '%/api/health'"
}
}
]
}
}
}
],
"Enrich": [
"WithProcessName"
],
"Properties": {
"Application": "Sample",
"Environment": "Test"
}
}
public class MyCommandHandler : IRequestHandler<MyCommand, Unit>
{
private readonly ILogger _myLogger;
private static int _count;
public MyCommandHandler()
{
_myLogger = Log.ForContext<IMyLogger>();
}
public async Task<Unit> Handle(MyCommand request, CancellationToken cancellationToken)
{
_count++;
Log.Debug("MyCommandHandler Count call = {count}",_count ); //write sample.txt
Log.Information("MyCommandHandler Count call = {count}",_count ); //write in sample.txt
Log.Error("MyCommandHandler Count call = {count}",_count); //write in sample.txt
_myLogger.Information("Log from IMyLogger", _count); //write in sample.txt and in sampleMy.txt
return Unit.Value;
}
}