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?
Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets.
Installing Serilog is simple. First, we open the NuGet Package Manager and search for the Serilog. AspNetCore package and install the latest stable version. After a few seconds, Serilog is installed.
NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent . NET platforms. Unlike other logging libraries, Serilog is built with powerful structured event data in mind.
Serilog defines several levels of log events. From low to high, these are Verbose , Debug , Information , Warning , Error and Fatal . You can set the minimum level you want to log, meaning that events for that level or higher will be logged.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With