Esempio n. 1
0
 private static bool CheckHostingEnvironmentIsProductionOrStagingOrDevelopment(
     Microsoft.Extensions.Hosting.IHostingEnvironment hostingEnvironment)
 {
     return(hostingEnvironment.IsProduction() ||
            hostingEnvironment.IsStaging() ||
            hostingEnvironment.IsDevelopment());
 }
Esempio n. 2
0
        public Startup(IConfiguration configuration, Microsoft.Extensions.Hosting.IHostingEnvironment hostingEnvironment)
        {
            _configuration = configuration;


            IConfigurationBuilder builder;

            if (hostingEnvironment.IsDevelopment())
            {
                builder = new ConfigurationBuilder()
                          .SetBasePath(hostingEnvironment.ContentRootPath)
                          .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);
            }
            else if (hostingEnvironment.IsProduction())
            {
                builder = new ConfigurationBuilder()
                          .SetBasePath(hostingEnvironment.ContentRootPath)
                          .AddJsonFile("appsettings.Production.json", optional: true, reloadOnChange: true);
            }
            else if (hostingEnvironment.IsStaging())
            {
                builder = new ConfigurationBuilder()
                          .SetBasePath(hostingEnvironment.ContentRootPath)
                          .AddJsonFile("appsettings.Staging.json", optional: true, reloadOnChange: true);
            }
            else if (hostingEnvironment.EnvironmentName == "Test")
            {
                builder = new ConfigurationBuilder()
                          .SetBasePath(hostingEnvironment.ContentRootPath)
                          .AddJsonFile("appsettings.Test.json", optional: true, reloadOnChange: true);
            }
            else
            {
                throw new Exception("NO ENVIRONMENT SET!");
            }

            var settings = configuration.GetSection("Logging").GetSection("LogLevel").GetValue("Default", "Information");

            var levelSwitch = new LoggingLevelSwitch();

            levelSwitch.MinimumLevel = settings switch
            {
                "Debug" => LogEventLevel.Debug,
                "Information" => LogEventLevel.Information,
                "Warning" => LogEventLevel.Warning,
                "Error" => LogEventLevel.Error,
                _ => LogEventLevel.Information,
            };
            var elasticUri = configuration["elasticsearch:uri"];

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.ControlledBy(levelSwitch)
                         .Enrich.FromLogContext()
                         .Enrich.WithProperty("Application", "SIGMA")
                         .Enrich.WithProperty("Environment", hostingEnvironment.EnvironmentName)
                         .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticUri))
            {
                AutoRegisterTemplate = true,
            })
                         .CreateLogger();
        }