コード例 #1
0
        public static IWebHostBuilder UseSerilog(this IWebHostBuilder hostBuilder,
                                                 Action <WebHostBuilderContext, ILoggingBuilder, LoggerConfiguration> configureLogging = default)
        {
            hostBuilder.ConfigureLogging((hostingContext, loggingBuilder) =>
            {
                var serilogBuilder = new SerilogBuilder(hostingContext.Configuration)
                                     .WithEnvironment(hostingContext.HostingEnvironment.EnvironmentName)
                                     .WithConfig(configuration =>
                {
                    configureLogging?.Invoke(hostingContext, loggingBuilder, configuration);
                    return(configuration);
                });

                Log.Logger = serilogBuilder.Build();
                GlobalLog.SetSerilog(Log.Logger);

                loggingBuilder.Services.AddSingleton(Log.Logger);
            });

            return(hostBuilder);
        }