Serilog not working on Host initialization
klyse opened this issue · 5 comments
I'm using the following nuget packages:
<PackageReference Include="Serilog.Extensions.Hosting" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
Following the documentation example the logger works:
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return new HostBuilder()
.UseSerilog();
}
}
However if I use my preferred way (also documented):
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console());
Same situation if i use the appsettings.json:
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext();
});
Expected behavior: log messages appear on the console.
Actual behavior: no log on console.
Could you share your configuration (appsettings.json) please - bear in mind that the format required by Serilog's ReadFrom.Configuration()
method is different to the default ASP.NET Core logging configuration.
okay, after further trouble shooting I found the error somewhere in the config.
The issue that led me on the wrong path is the following:
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console());
The logger does not work if the hostingContext.Configuration
is either empty or not valid. Even if the .WriteTo.Console()
method gets called. Correct?
Same for this example:
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.WriteTo.Console();
});
This way I figured that it's a logger problem and not configuration related.
I am not sure what's going on, here - some more info might be needed, if you think there's a problem in the Serilog code causing this. Cheers!
Hi @nblumhardt,
never mind :). I was only confused with the configuration.
Best regards
Klyse
I have interesting issue using rabbitmq using generic host
running IHostedService and if i use UseSerilog(with configuration etc) rabbit doesnt catch message (most of the time, sometimes at start it does get a single message)
using following (tried using all 3 versions of hosting)
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.4" />
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="2.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Graylog" Version="2.1.3" />