Reputation: 61646
P.S. The code below works fine. The problem was between the monitor and the chair. Answer by @jpgrassi sent me in the right direction to resolve it.
I have the following in program.cs:
public class Program {
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
.UseStartup<Startup>();
}
And in appsettings.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Default": "Information",
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "log-{Date}.txt",
}
}
]
},
"AllowedHosts": "*"
}
In the controller, I write out a log _logger.LogWarning("foo");
, but the file is not written out anywhere, there aren't any error that I can see.
I've imported following packages from Nuget: Serilog, Serilog.AspNetCore, Serilog.Settings.Configuration, Serilog.Sinks.RollingFile.
What am I missing?
Upvotes: 8
Views: 12674
Reputation: 5762
I posted the original answer assuming the problem was the lack of the Using
property in the Serilog configuration. I always had it in my projects and it was the only thing missing in OP's settings. But, after posting the answer I tried different configuration alternatives, including the same as in the question and the logs were still being produced. Reading more, I found out that the Using
property is not necessary. From the serilog-settings-configuration
package:
(This package implements a convention using DependencyContext to find any package with Serilog anywhere in the name and pulls configuration methods from it, so the Using example above is redundant.) Source: https://github.com/serilog/serilog-settings-configuration
As it turns out (see question comments) the real issue was log file location and write permissions. Code-wise everything was already working.
I'll keep the original answer here, as a matter of history, but its content does not actually fix anything, as nothing was broken in the first place.
Original answer:
I believe the problem is because you did not specify in your appsettings
to actually use the RollingFile
sink. I just quickly created a new app and it works:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFile" ],
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "Console" },
{
"Name": "RollingFile",
"Args": { "pathFormat": "log-{Date}.txt" }
}
],
"Properties": {
"Application": "Sample"
}
}
Using the following NuGet packages:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>
This logs to a file in the root directory of the app log-20190304.txt
.
Upvotes: 6