saleem-mirza / serilog-sinks-mysql

Serilog sink that writes to MySQL database.
Apache License 2.0
35 stars 25 forks source link

The master branch not work with asp.net core #13

Open JasonLiuLiuLiuLiu opened 5 years ago

JasonLiuLiuLiuLiu commented 5 years ago

Hi saleem,

it seems master banch can't work with asp.net core, and the latest nuget also not work, but when i switch to dev branch, it worked. I tried to find the root cause but didn't find it. Do you know the reason?

JasonLiuLiuLiuLiu commented 5 years ago

there is the program code.

    public class Program
    {
        public static void Main(string[] args)
        {
            args.SetConfigInfo();

            var logConfig = new LoggerConfiguration()
#if DEBUG
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
#else
                .MinimumLevel.Information()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
#endif
                .Enrich.FromLogContext();

            logConfig = ConfigDataHelper.TryGetConnectionString("iBlogs", out var connectionString) ?
                logConfig.WriteTo.MySQL(connectionString) : logConfig.WriteTo.Console();

            Log.Logger = logConfig.CreateLogger();

            try
            {
                Log.Information("Starting web host");
                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .AddCommandLine(args);
                })
                .UseStartup<Startup>()
                .UseSerilog();
    }