oakton
oakton copied to clipboard
Usage breaks logging
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Oakton;
using Serilog;
using Serilog.Events;
namespace LegacyEnterpriseSync
{
public class Program
{
public static Task<int> Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting web host");
return CreateHostBuilder(args).RunOaktonCommands(args);
// CreateHostBuilder(args).Build().Run();
// return Task.FromResult(0);
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return Task.FromResult(1);
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host
.CreateDefaultBuilder(args)
.UseSerilog((context, servicesProvider, loggingConfig) =>
{
loggingConfig.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console();
})
.ConfigureAppConfiguration(configBuilder => { configBuilder.AddEnvironmentVariables(); })
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}
The app runs, but request logging, etc. is not printing to the console. Only that first message before the Oakton function's call.
When I instead use:
CreateHostBuilder(args).Build().Run();
return Task.FromResult(0);
All the logging takes place as expected. I have tried with and without the 2 stage logger initialization using the lambda:
(context, servicesProvider, loggingConfig) =>
{
loggingConfig.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console();
}
I'm observing the same behavior in an app that also uses Serilog.
@yehudamakarov , try adding preserveStaticLogger: true
inside UseSerilog
after the lambda as directed here. It fixed my issue using Oakton and Serilog together.
@yehudamakarov This sounds like the same issue as I had over on the Jasper repo. See my open issue: https://github.com/JasperFx/jasper/issues/668
I found a workaround (it's referenced in a comment on that item) that sounds like it will fix your issue.
I'm gonna tell y'all it's on Serilog. This only happens in combination with Serilog.
@flipdoubt LAte to the party here, but I'll add that to the Oakton docs when I update them next week
I finally added something to the docs today just pointing at the StackOverflow page w/ the workaround, will publish soon. Nothing actionable in Oakton itself here, this is really a Serilog issue.