示例#1
0
        private static void SetupLogglyConfiguration(LogglySettings logglySettings)
        {
            ILogglyConfig config = LogglyConfig.Instance;

            config.CustomerToken   = logglySettings.CustomerToken;
            config.ApplicationName = logglySettings.ApplicationName;
            config.Transport       = new TransportConfiguration()
            {
                EndpointHostname = logglySettings.EndpointHostname,
                EndpointPort     = logglySettings.EndpointPort,
                LogTransport     = logglySettings.LogTransport
            };
            config.ThrowExceptions = logglySettings.ThrowExceptions;
            config.TagConfig.Tags.AddRange(new ITag[] {
                new ApplicationNameTag {
                    Formatter = "Application-{0}"
                },
                new HostnameTag {
                    Formatter = "Host-{0}"
                }
            });
        }
示例#2
0
        public static async Task <IHostBuilder> RunWithSerilogAsync(this IHostBuilder hostBuilder)
        {
            Environment.SetEnvironmentVariable("SEQ_URL", "http://localhost:5341");
            string seqEnvironmentValue = Environment.GetEnvironmentVariable("SEQ_URL");

            using (Logger logger = new LoggerConfiguration()
                                   .ReadFrom.Configuration(Configuration)
                                   .MinimumLevel.Information()
                                   .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
                                   .MinimumLevel.Override("Autofac.Analysis", LogEventLevel.Warning)
                                   .Destructure.ToMaximumDepth(100)
                                   .Enrich.FromLogContext()
                                   .WriteTo.Debug()
                                   .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
                                   .WriteTo.File(
                       //new RenderedCompactJsonFormatter(),
                       path: "Logs/audit.txt",
                       rollingInterval: RollingInterval.Day,
                       retainedFileCountLimit: 8,
                       fileSizeLimitBytes: 107341824,
                       rollOnFileSizeLimit: true,
                       outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                       shared: true
                       )
                                   .WriteTo.Loggly()
                                   .WriteTo.Seq(Environment.GetEnvironmentVariable("SEQ_URL") ?? "http://localhost:5341")
                                   .WriteTo.Async(a => a.RollingFile("Logs/SimpleMVC-{Date}.log", LogEventLevel.Information))
                                   .CreateLogger())
            {
                Log.Logger = logger;

                try
                {
                    Log.Warning("Starting up .. !!!!");

                    hostBuilder.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                                           .ReadFrom.Configuration(hostingContext.Configuration)
                                           .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
                                           .MinimumLevel.Override("Autofac.Analysis", LogEventLevel.Warning)
                                           .Destructure.ToMaximumDepth(100)
                                           .Enrich.FromLogContext()
                                           .WriteTo.Debug()
                                           .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
                                           .WriteTo.File(new RenderedCompactJsonFormatter(), "/logs/log.ndjson")
                                           .WriteTo.Seq(Environment.GetEnvironmentVariable("SEQ_URL") ?? "http://localhost:5341")
                                           );


                    ContainerBuilder containerBuilder = new ContainerBuilder();
#if DEBUG
                    containerBuilder.RegisterModule(new AnalysisModule(logger));
#endif

                    LogglySettings logglySettings = new LogglySettings();
                    Configuration.GetSection("Serilog:Loggly").Bind(logglySettings);
                    SetupLogglyConfiguration(logglySettings);

                    Log.Information("Started Succesfully .. !!!!");

                    await hostBuilder.Build().RunAsync();
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Host terminated unexpectedly");
                    Log.Fatal(ex, "Application start-up failed");
                }
                finally
                {
                    Log.CloseAndFlush();
                }
            }

            return(hostBuilder);
        }