Exemplo n.º 1
0
        public static void ConfigureLogging(IConfiguration configuration)
        {
            lock (_lock)
            {
                LoggerConfiguration config = new LoggerConfiguration();

                // load from config if section exists
                if (configuration.GetSection(_sectionName).Exists())
                {
                    config.ReadFrom.Configuration(configuration, _sectionName);
                    // configure datadog
                    DatadogOptions ddOptions = configuration.GetSection(_sectionName).GetSection("DataDog").Get <DatadogOptions>();
                    if (!string.IsNullOrWhiteSpace(ddOptions?.ApiKey))
                    {
                        config.WriteTo.DatadogLogs(
                            ddOptions.ApiKey,
                            source: ".NET",
                            service: ddOptions.ServiceName ?? "WsbcDiscordBot",
                            host: ddOptions.HostName ?? Environment.MachineName,
                            new string[] { },
                            ddOptions.ToDatadogConfiguration(),
                            // no need for debug logs in datadag
                            logLevel: ddOptions.OverrideLogLevel ?? LogEventLevel.Information
                            );
                    }
                }
                // otherwise use defaults
                else
                {
                    config.WriteTo.Console()
                    .MinimumLevel.Is(Debugger.IsAttached ? LogEventLevel.Verbose : LogEventLevel.Information)
                    .Enrich.FromLogContext();
                }


                // create the logger
                Serilog.Log.Logger = config.CreateLogger();

                // enable logging of unhandled exceptions, but only when initializing for the first time
                if (!_initialized)
                {
                    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
                }

                _initialized = true;
            }
        }
Exemplo n.º 2
0
        private static void ConfigureSerilog(HostBuilderContext context, LoggerConfiguration config)
        {
            config.ReadFrom.Configuration(context.Configuration);
            DatadogOptions ddOptions = context.Configuration.GetSection("Serilog")?.GetSection("DataDog")?.Get <DatadogOptions>();

            if (ddOptions != null)
            {
                config.WriteTo.DatadogLogs(
                    ddOptions.ApiKey,
                    source: ".NET",
                    service: ddOptions.ServiceName ?? "WolfBots-Size",
                    host: ddOptions.HostName ?? Environment.MachineName,
                    new string[] {
                    $"env:{(ddOptions.EnvironmentName ?? context.HostingEnvironment.EnvironmentName)}",
                    $"assembly:{(ddOptions.AssemblyName ?? context.HostingEnvironment.ApplicationName)}"
                },
                    ddOptions.ToDatadogConfiguration(),
                    // no need for debug logs in datadag
                    logLevel: ddOptions.OverrideLogLevel ?? LogEventLevel.Verbose
                    );
            }
        }
        public static void ConfigureSerilog(HostBuilderContext context, LoggerConfiguration config)
        {
            config.ReadFrom.Configuration(context.Configuration)
            .Enrich.FromLogContext();
            DatadogOptions ddOptions = context.Configuration.GetSection("Serilog")?.GetSection("DataDog")?.Get <DatadogOptions>();

            if (!string.IsNullOrWhiteSpace(ddOptions?.ApiKey))
            {
                config.WriteTo.DatadogLogs(
                    ddOptions.ApiKey,
                    source: ".NET",
                    service: ddOptions.ServiceName ?? "Einherji",
                    host: ddOptions.HostName ?? Environment.MachineName,
                    new string[] {
                    $"env:{(ddOptions.EnvironmentName ?? context.HostingEnvironment.EnvironmentName)}",
                    $"assembly:{(ddOptions.AssemblyName ?? context.HostingEnvironment.ApplicationName)}"
                },
                    ddOptions.ToDatadogConfiguration(),
                    // no need for debug logs in datadag
                    logLevel: ddOptions.OverrideLogLevel ?? LogEventLevel.Information
                    );
            }
        }