var logConfig = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.ColoredConsole(LogEventLevel.Error, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
.WriteTo.Logger(
c => c.WriteTo.ColoredConsole(LogEventLevel.Information, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
.Filter.ByIncludingOnly("SourceContext = 'Microsoft.Hosting.Lifetime'"))
.WriteTo.File(
"logs\\log-.log",
LogEventLevel.Information,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{SourceContext}\t{Message:lj}{NewLine}{Exception}")
.WriteTo.Logger(
domainLogConfig => domainLogConfig
.WriteTo.File(
"logs\\domain\\log-.log",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{DomainContext}\t{Message:lj}{NewLine}")
.Filter.ByIncludingOnly("DomainContext is not null"))
.CreateLogger();
builder.AddSerilog(logConfig, true);
"Serilog": {
"Using": [
"Serilog",
"Serilog.Sinks.File",
"Serilog.Sinks.ColoredConsole",
"Serilog.Filters.Expressions"
],
"Enrich": [
"FromLogContext"
],
"MinimumLevel": "Verbose",
"WriteTo": [
{
"Name": "ColoredConsole",
"Args": {
"restrictedToMinimumLevel": "Error",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "ColoredConsole",
"Args": {
"restrictedToMinimumLevel": "Information",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}"
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "SourceContext = 'Microsoft.Hosting.Lifetime'"
}
}
]
}
}
},
{
"Name": "File",
"Args": {
"path": "logs\\log-.log",
"restrictedToMinimumLevel": "Information",
"rollingInterval": "Day",
"rollOnFileSizeLimit": "True",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{SourceContext}\t{Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "logs\\domain\\log-.log",
"restrictedToMinimumLevel": "Information",
"rollingInterval": "Day",
"rollOnFileSizeLimit": "True",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}\t{Level:u3}\t{DomainContext}\t{Message:lj}{NewLine}"
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "DomainContext is not null"
}
}
]
}
}
}
]
}