I have the following in my ASPCoreNet project:
appsetting.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "logs/log-.txt",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}"
}
}
]
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Enrich": [ "WithMachineName", "WithThreadId", "FromLogContext" ]
},
"AllowedHosts": "*"
}
However, when I read from the configuration my logger doesn't work:
public class MyLogger
{
public static ILogger LoggingInstance
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
}
}
However, this seems to work:
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(a => a.File(
outputTemplate: ""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}",
path: "logs/log-.txt",
rollingInterval: RollingInterval.Day
)).CreateLogger();
When I attempt to make an async logger from config, it does nothing. When I do the same thing inling it, it works just fine.
I actually did have to wrap async into file but was doing it incorrectly:
{
"Serilog":{
"Using":[
"Serilog",
"Serilog.Sinks.File",
"Serilog.Sinks.Async"
],
"MinimumLevel":"Debug",
"WriteTo:Async":{
"Name":"Async",
"Args":{
"configure":[
{
"Name":"File",
"Args":{
"path":"project-log.txt",
"rollingInterval":"Day"
}
}
]
}
}
}
}