Serilog setup for Asp.Net Core 2.0.1 not producing logs
which was fairly obvious because in the bin folder the file isn't called appsettings.json, its called TestMvcApp.runtimeconfig.json.
TestMvcApp.runtimeconfig.json
is not your appsettings.json
, it's a runtime configuration file as it's clear from the file name.
I bet that your appsettings.json
is just not copied to the output directory during the build. To fix that select appsettings.json
in Visual Studio Solution explorer, in context menu select properties and set 'Copy to Output Directory' to 'Copy always' or 'Copy if newer':
Also your json configuration has several issues:
Serilog
section should not be insideApplicationConfiguration
section. It should be on the same level, i.e top-level section.- You also has suspicious WriteTo section with one sink (
RollingFile
) inside another (Async
). Check here sample json configuration for Serilog. - In order to use all those sinks (
Serilog.Sinks.RollingFile
,Serilog.Sinks.Console
) you should install corresponding sink NuGets: Serilog.Sinks.RollingFile, Serilog.Sinks.Console, etc.
Here is configuration file (with RollingFile sink only) that works fine for me:
{ "ApplicationConfiguration": { "ConnectionStrings": { "DevelopmentConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo_Development;Trusted_Connection=True;MultipleActiveResultSets=true" }, "ApplicationInfo": { "VersionNumber": "1.0.0", "Author": "Jimbo", "ApplicationName": "CustomTemplate", "CreatedOn": "November 20, 2017" } }, "Serilog": { "Using": [ "Serilog.Sinks.RollingFile" ], "MinimumLevel": { "Default": "Information", "Override": { "Microsoft": "Warning" } }, "WriteTo": [ { "Name": "RollingFile", "Args": { "pathFormat": "c:\\log-{Date}.log" } } ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { "Application": "CustomTemplate" } }}
The accepted answer only logged some messages for my project. I'm not sure why. Ondrej Balas's approach worked better for me.
Install these Nuget packages:
- Serilog
- Serilog.AspNetCore
- Serilog.Settings.Configuration
- Serilog.Sinks.Console
- Serilog.Sinks.RollingFile
Program.cs needs .UseSerilog()
added:
public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); }) .UseStartup<Startup>() .UseSerilog() .Build();
Startup.cs needs a couple of changes
one in the constructor:
public Startup(IConfiguration configuration) { Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger(); Configuration = configuration; }
another in Configure()
:
loggerFactory.AddSerilog(); if (env.IsDevelopment()) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); }
appsettings.json needs a Serilog config entry in root:
"Serilog": { "Using": [ "Serilog.Sinks.Console" ], "MinimumLevel": { "Default": "Information", "Override": { "Microsoft": "Warning", "System": "Warning" } }, "WriteTo": [ { "Name": "Console" }, { "Name": "RollingFile", "Args": { "pathFormat": "logs\\log-{Date}.log", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" } } ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { "Application": "My Application" }}