I'm trying to use Serilog in a dotnet webapi app. I need logging to go to both the Console and a Rolling log file.
Everything is coming out to the Console but nothing is appearing in the rolling log file and I'm not getting any errors anywhere.
I have setup serilog in my code like so:
// in program.cs Main
var configuration = new ConfigurationBuilder()
.SetBasePath(BASEDIR)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.With(new ThreadIdEnricher())
.CreateLogger();
// startup.cs Configure
app.UseSerilogRequestLogging();
And in appsetting.json I have
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore.Mvc.Internal": "Warning",
"Microsoft.AspNetCore.Authentication": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u1} TID:{ThreadId} {Message:lj}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console"
}
}
]
}
},
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "c:\\logs\\{Date}.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u1} TID:{ThreadId} {Message:lj}{NewLine}{Exception}",
"buffered": false,
"rollingInterval": "Serilog.RollingInterval.Day, Serilog.Sinks.File",
"retainedFileCountLimit": 7
}
}
]
}
}
]
}
}
have you tried to change to static file name? does the path exists? it seems that the configuration on the readme from their github page is a bit differnt from yours: https://github.com/serilog/serilog-sinks-file#user-content-json-appsettingsjson-configuration:~:text=node%2C%20%3A-,%7B,%7D,-See%20the%20XML also maybe try to work your way back from setting it up in code then move back to config file?