Exemple #1
0
        public static void Main(string[] args)
        {
            //Build config to get config files
            var config = new ConfigurationBuilder()
                         .SetBasePath(Environment.CurrentDirectory)
                         .AddJsonFile("appsettings.json", optional: false)
                         .Build();

            var instrumentationConfig = new InstrumentationConfiguration();

            //Map instrumentationConfiguration section into config file
            config.GetSection(nameof(InstrumentationConfiguration)).Bind(instrumentationConfig);

            //Initiate config with configSection
            Log.Logger = LoggingConfiguration.GetConfiguration(instrumentationConfig).CreateLogger();

            try
            {
                Log.Information("Starting API host");

                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception e)
            {
                Log.Fatal(e, "API host could not start or terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
 public static Serilog.LoggerConfiguration GetConfiguration(InstrumentationConfiguration config)
 {
     return(new LoggerConfiguration()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .MinimumLevel.Override("System", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
            .Enrich.WithEnvironment("ASPNETCORE_ENVIRONMENT")
            .Enrich.FromLogContext()
            .Enrich.WithThreadId()
            .WriteTo.Console()
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(config.ElasticsearchUrl))
     {
         AutoRegisterTemplate = true,
         ModifyConnectionSettings = x => x.BasicAuthentication(config.Username, config.Password)
     })
            .WriteTo.ApplicationInsightsEvents(TelemetryConfiguration.Active));
 }