C
C#2y ago
mixels

❔ Serilog.Settings.Configuration file sink not writing to file?

I'm using the below configuration for Serilog trying to get it to write to both console and file, but it's only writing to console. Does anyone see what's wrong? Been looking at this for awhile now and would appreciate a second set of eyes. JSON:
{
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/logs/TestWorker.log",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] | {Message}{NewLine}{Exception}"
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] | {Message}{NewLine}{Exception}"
}
}
],
"Application": "The7LegionsVoteHelper"
}
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/logs/TestWorker.log",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] | {Message}{NewLine}{Exception}"
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] | {Message}{NewLine}{Exception}"
}
}
],
"Application": "The7LegionsVoteHelper"
}
}
Code (Program class):
private static async Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
builder.ClearProviders();
builder.AddSerilog();
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();

IConfiguration configuration = host.Services.GetRequiredService<IConfiguration>();

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

await host.RunAsync();
}
private static async Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
builder.ClearProviders();
builder.AddSerilog();
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();

IConfiguration configuration = host.Services.GetRequiredService<IConfiguration>();

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

await host.RunAsync();
}
2 Replies
antariksh
antariksh2y ago
I quickly created a new project and tried myself. It logs to both console and file. It's worth noting that the path you have given for file is absolute path, so your log file will be generated at the root of your drive. e.g. my code is present at C:\user\source\repos\Worker1, so log file is generated at C:\logs\TestWorker.log
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?