oakton icon indicating copy to clipboard operation
oakton copied to clipboard

Usage breaks logging

Open yehudamakarov opened this issue 3 years ago • 5 comments

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();
}

yehudamakarov avatar Apr 09 '21 15:04 yehudamakarov

I'm observing the same behavior in an app that also uses Serilog.

flipdoubt avatar May 11 '21 21:05 flipdoubt

@yehudamakarov , try adding preserveStaticLogger: true inside UseSerilog after the lambda as directed here. It fixed my issue using Oakton and Serilog together.

flipdoubt avatar May 15 '21 18:05 flipdoubt

@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.

TheFellow avatar May 26 '21 00:05 TheFellow

I'm gonna tell y'all it's on Serilog. This only happens in combination with Serilog.

jeremydmiller avatar Nov 27 '21 23:11 jeremydmiller

@flipdoubt LAte to the party here, but I'll add that to the Oakton docs when I update them next week

jeremydmiller avatar Nov 27 '21 23:11 jeremydmiller

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.

jeremydmiller avatar Oct 26 '22 19:10 jeremydmiller