Ejemplo n.º 1
0
        private static void ConfigureBootstrapLogging(
            string environment
            )
        {
            var configuration = new LoggerConfiguration()
                                .MinimumLevel.Override("Microsoft", LogEventLevel.Information);

            ConfigureLogging(configuration, environment);
            Log.Logger = configuration.CreateBootstrapLogger();
        }
Ejemplo n.º 2
0
    public static IHostBuilder CreateHostBuilder(IConfigurationRoot config, string environment)
    {
        Console.Title = "Exceptionless Web";

        var options = AppOptions.ReadFromConfiguration(config);

        var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config)
                           .Enrich.FromLogContext()
                           .Enrich.WithMachineName()
                           .Enrich.WithSpan();

        if (!String.IsNullOrEmpty(options.ExceptionlessApiKey))
        {
            loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Information);
        }

        Log.Logger = loggerConfig.CreateBootstrapLogger();
        var configDictionary = config.ToDictionary("Serilog");

        Log.Information("Bootstrapping Exceptionless Web in {AppMode} mode ({InformationalVersion}) on {MachineName} with settings {@Settings}", environment, options.InformationalVersion, Environment.MachineName, configDictionary);

        var builder = Host.CreateDefaultBuilder()
                      .UseEnvironment(environment)
                      .UseSerilog()
                      .ConfigureWebHostDefaults(webBuilder => {
            webBuilder
            .UseConfiguration(config)
            .ConfigureKestrel(c => {
                c.AddServerHeader = false;

                if (options.MaximumEventPostSize > 0)
                {
                    c.Limits.MaxRequestBodySize = options.MaximumEventPostSize;
                }
            })
            .UseStartup <Startup>();
        })
                      .ConfigureServices((ctx, services) => {
            services.AddSingleton(config);
            services.AddAppOptions(options);
            services.AddHttpContextAccessor();
            services.AddApm(new ApmConfig(config, "Exceptionless.Web", "Exceptionless", options.InformationalVersion, options.CacheOptions.Provider == "redis"));
        });

        if (!String.IsNullOrEmpty(options.MetricOptions.Provider))
        {
            ConfigureMetricsReporting(builder, options.MetricOptions);
        }

        return(builder);
    }
    /// <summary>
    ///     Create and configure the logger.
    /// </summary>
    /// <remarks>
    ///     Additional Serilog services are registered during <see cref="HostBuilderExtensions.ConfigureUmbracoDefaults" />.
    /// </remarks>
    public static IServiceCollection AddLogger(
        this IServiceCollection services,
        IHostEnvironment hostEnvironment,
        IConfiguration configuration)
    {
        // TODO: WEBSITE_RUN_FROM_PACKAGE - can't assume this DIR is writable - we have an IConfiguration instance so a later refactor should be easy enough.
        var loggingDir = hostEnvironment.MapPathContentRoot(Constants.SystemDirectories.LogFiles);
        ILoggingConfiguration loggingConfig = new LoggingConfiguration(loggingDir);

        var umbracoFileConfiguration = new UmbracoFileConfiguration(configuration);

        services.TryAddSingleton(umbracoFileConfiguration);
        services.TryAddSingleton(loggingConfig);
        services.TryAddSingleton <ILogEventEnricher, ApplicationIdEnricher>();

        ///////////////////////////////////////////////
        // Bootstrap logger setup
        ///////////////////////////////////////////////

        LoggerConfiguration serilogConfig = new LoggerConfiguration()
                                            .MinimalConfiguration(hostEnvironment, loggingConfig, umbracoFileConfiguration)
                                            .ReadFrom.Configuration(configuration);

        Log.Logger = serilogConfig.CreateBootstrapLogger();

        ///////////////////////////////////////////////
        // Runtime logger setup
        ///////////////////////////////////////////////

        services.AddSingleton(sp =>
        {
            var logger = new RegisteredReloadableLogger(Log.Logger as ReloadableLogger);

            logger.Reload(cfg =>
            {
                cfg.MinimalConfiguration(hostEnvironment, loggingConfig, umbracoFileConfiguration)
                .ReadFrom.Configuration(configuration)
                .ReadFrom.Services(sp);

                return(cfg);
            });

            return(logger);
        });

        services.AddSingleton <ILogger>(sp =>
        {
            ILogger logger = sp.GetRequiredService <RegisteredReloadableLogger>().Logger;
            return(logger.ForContext(new NoopEnricher()));
        });

        services.AddSingleton <ILoggerFactory>(sp =>
        {
            ILogger logger = sp.GetRequiredService <RegisteredReloadableLogger>().Logger;
            return(new SerilogLoggerFactory(logger));
        });

        // Registered to provide two services...
        var diagnosticContext = new DiagnosticContext(Log.Logger);

        // Consumed by e.g. middleware
        services.TryAddSingleton(diagnosticContext);

        // Consumed by user code
        services.TryAddSingleton <IDiagnosticContext>(diagnosticContext);

        return(services);
    }