public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>(); webBuilder.UseSerilog((hostingContext, builder) => { var serilog = new Ocean.Api.Extensions.Serilog(); hostingContext.Configuration.GetSection("Serilog").Bind(serilog); builder.CreateLogConfig(serilog); }); });
public static LoggerConfiguration CreateLogConfig(this LoggerConfiguration config, Serilog options) { string LogFilePath(string LogEvent) => string.Format(options.Path, LogEvent); string SerilogOutputTemplate = options?.Template + new string('-', 50); var LogEventLevel = GetMinimumLogLevel(options.MinLevel); config?.Enrich.FromLogContext() .WriteTo.Console() .MinimumLevel.Is(LogEventLevel) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(LogFilePath("Information"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)); return(config); }