'WriteTo.Seq' not working with new 'CreateBootstrapLogger'
MJeorrett opened this issue · 3 comments
Hi,
First off, the new CreateBootstrapLogger
looks great and this promises to greatly simplify bootstrap code. However I am bumping in to an issue and I am not sure if I have misunderstood something or there is a bug in the new feature.
I have code in my Program.cs as listed at the end of this issue.
Expected result
Asp.Net core bootstrapping logs are written to both Seq and Console.
Actual result
Asp.Net core bootstrapping logs are only written to the Console. The only log which is written to Seq is 'Starting service.'.
Code
public class Program
{
public readonly static AssemblyName AssemblyName = Assembly.GetEntryAssembly().GetName();
/// CLI Startup routine
public static int Main(string[] args)
{
var loggerConfig = new LoggerConfiguration()
.WriteTo.Console();
var seqUrl = Environment.GetEnvironmentVariable("SEQ_URL");
if (!string.IsNullOrEmpty(seqUrl))
{
loggerConfig.WriteTo.Seq(seqUrl);
}
Log.Logger = loggerConfig.CreateBootstrapLogger();
Log.Information("Starting service.");
try
{
var host = BuildWebHost(args);
MigrateDatabase(host);
host.Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly.");
throw;
}
finally
{
// TODO: This is not getting logged at the moment.
Log.Information("Terminated.");
Log.CloseAndFlush();
}
}
private static void MigrateDatabase(IHost host)
{
// Omitted for brevity.
}
private static IHost BuildWebHost(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, loggerConfig) =>
{
loggerConfig
.Enrich.WithProperty("AssemblyName", AssemblyName.Name.ToString())
.Enrich.WithProperty("AssemblyVersion", AssemblyName.Version.ToString())
.ReadFrom.Configuration(context.Configuration);
#if DEBUG
Log.Information("Compiled with 'DEBUG' flag so enriching with debugger attached property.");
loggerConfig.Enrich.WithProperty("DebuggerAttached", Debugger.IsAttached);
#endif
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseStartup<Startup>();
})
.Build();
}
}
Thanks for the note. Does your JSON config file contain configuration for the Seq sink?
If not, that will be the issue. The UseSerilog()
callback completely replaces the logging pipeline, so you will need to reconfigure the Seq sink there if you want subsequent logs to go to Seq:
return Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, loggerConfig) =>
{
loggerConfig
.Enrich.WithProperty("AssemblyName", AssemblyName.Name.ToString())
.Enrich.WithProperty("AssemblyVersion", AssemblyName.Version.ToString())
.WriteTo.Seq("https://seq.example.com") // <- here
.ReadFrom.Configuration(context.Configuration);
You'd need to rearrange the code in the callback to check for the existence of SEQ_URL
in the environment, but the above should be the general gist.
Hope this helps,
Nick
Thanks for the quick response Nick. This was indeed the problem and adding the seq config to the UseSerilog
configuration fixed the issue.
It may just be me that assumed that UseSerilog
is additive to the mutable logger but I wonder if it could be made clearer that the logging pipeline is replaced? Or perhaps you are trying to keep the API the same as far as possible in this new version?
In any case, this is a great feature!
Thanks for the notes