JasperFx / oakton

Parsing and Utilities for Command Line Tools in .Net
http://jasperfx.github.io/oakton
Apache License 2.0
308 stars 41 forks source link

Usage breaks logging #51

Closed yehudamakarov closed 2 years ago

yehudamakarov commented 3 years ago
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();
}
flipdoubt commented 3 years ago

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

flipdoubt commented 3 years ago

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

TheFellow commented 3 years ago

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

jeremydmiller commented 3 years ago

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

jeremydmiller commented 3 years ago

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

jeremydmiller commented 2 years ago

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.