예제 #1
0
 public static IWebHostBuilder UseSerilog(
     this IWebHostBuilder webHostBuilder,
     string appName,
     bool?serverCertificateValidationOverride = null)
 {
     webHostBuilder.UseSerilog((context, loggerConfiguration) => loggerConfiguration
                               .MinimumLevel.Debug()
                               .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                               .Enrich.FromLogContext()
                               .WriteTo.Console()
                               .WriteTo.ApplicationInsights(
                                   telemetryConfiguration: TelemetryConfiguration.Active,
                                   telemetryConverter: TelemetryConverter.Traces)
                               .WriteTo.File(
                                   path: $"D:\\home\\LogFiles\\Application\\{appName}.txt",
                                   fileSizeLimitBytes: 1_000_000,
                                   shared: true,
                                   flushToDiskInterval: TimeSpan.FromSeconds(1),
                                   rollOnFileSizeLimit: true)
                               .WriteTo.Elasticsearch(
                                   options: new ElasticsearchSinkOptions(context.Configuration.GetLogNodes())
     {
         AutoRegisterTemplate        = true,
         AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
         IndexFormat              = $"{appName.ToLowerInvariant().Replace('.', '-')}-logs",
         CustomFormatter          = new ExceptionAsObjectJsonFormatter(renderMessage: true),
         ModifyConnectionSettings = x => x.ServerCertificateValidationCallback(
             (o, certificate, arg3, arg4) => serverCertificateValidationOverride ?? arg4 == SslPolicyErrors.None)
     }));
     return(webHostBuilder);
 }
예제 #2
0
 public static void Main(string[] args)
 {
     Log.Logger = new LoggerConfiguration()
                  .MinimumLevel.Information()
                  .Enrich.FromLogContext()
                  .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                  // new CompactJsonFormatter(),
                  .WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @$ "Logs", @$ "{DateTime.Now.Date.ToString(" yyyyMMdd ")}", @"log.txt"), rollingInterval: RollingInterval.Minute)
                  .WriteTo.Elasticsearch(new Serilog.Sinks.Elasticsearch.ElasticsearchSinkOptions(new Uri("http://localhost:9200/"))
     {
         AutoRegisterTemplate = true,
     })
                  .CreateLogger();
     CreateHostBuilder(args).Build().Run();
 }
        public static int Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.File(
                "./logs/time-tracker.log",
                fileSizeLimitBytes: 1_000_000,
                rollOnFileSizeLimit: true,
                shared: true,
                flushToDiskInterval: TimeSpan.FromSeconds(1))
                         .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://elasticsearch:9200/"))
            {
                AutoRegisterTemplate        = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                EmitEventFailure            = EmitEventFailureHandling.WriteToSelfLog
            })
                         .CreateLogger();
            SelfLog.Enable(Console.Error);

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