static DatadogLogging()
        {
            // No-op for if we fail to construct the file logger
            var     nullRateLimiter = new NullLogRateLimiter();
            ILogger internalLogger  = new LoggerConfiguration()
                                      .WriteTo.Sink <NullSink>()
                                      .CreateLogger();

            SharedLogger = new DatadogSerilogLogger(internalLogger, nullRateLimiter);

            try
            {
                if (GlobalSettings.Source.DebugEnabled)
                {
                    LoggingLevelSwitch.MinimumLevel = LogEventLevel.Debug;
                }

                var maxLogSizeVar = EnvironmentHelpers.GetEnvironmentVariable(ConfigurationKeys.MaxLogFileSize);
                if (long.TryParse(maxLogSizeVar, out var maxLogSize))
                {
                    // No verbose or debug logs
                    MaxLogFileSize = maxLogSize;
                }

                string logDirectory = null;
                try
                {
                    logDirectory = GetLogDirectory();
                }
                catch
                {
                    // Do nothing when an exception is thrown for attempting to access the filesystem
                }

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                if (logDirectory == null)
                {
                    return;
                }

                var domainMetadata = DomainMetadata.Instance;

                // Ends in a dash because of the date postfix
                var managedLogPath = Path.Combine(logDirectory, $"dotnet-tracer-managed-{domainMetadata.ProcessName}-.log");

                var loggerConfiguration =
                    new LoggerConfiguration()
                    .Enrich.FromLogContext()
                    .MinimumLevel.ControlledBy(LoggingLevelSwitch)
                    .WriteTo.File(
                        managedLogPath,
                        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Exception} {Properties}{NewLine}",
                        rollingInterval: RollingInterval.Day,
                        rollOnFileSizeLimit: true,
                        fileSizeLimitBytes: MaxLogFileSize,
                        shared: true);

                try
                {
                    loggerConfiguration.Enrich.WithProperty("MachineName", domainMetadata.MachineName);
                    loggerConfiguration.Enrich.WithProperty("Process", $"[{domainMetadata.ProcessId} {domainMetadata.ProcessName}]");
                    loggerConfiguration.Enrich.WithProperty("AppDomain", $"[{domainMetadata.AppDomainId} {domainMetadata.AppDomainName}]");
#if NETCOREAPP
                    loggerConfiguration.Enrich.WithProperty("AssemblyLoadContext", System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(typeof(DatadogLogging).Assembly)?.ToString());
#endif
                    loggerConfiguration.Enrich.WithProperty("TracerVersion", TracerConstants.AssemblyVersion);
                }
                catch
                {
                    // At all costs, make sure the logger works when possible.
                }

                internalLogger = loggerConfiguration.CreateLogger();
                SharedLogger   = new DatadogSerilogLogger(internalLogger, nullRateLimiter);

                var             rate        = GetRateLimit();
                ILogRateLimiter rateLimiter = rate == 0
                    ? nullRateLimiter
                    : new LogRateLimiter(rate);

                SharedLogger = new DatadogSerilogLogger(internalLogger, rateLimiter);
            }
            catch
            {
                // Don't let this exception bubble up as this logger is for debugging and is non-critical
            }
        }
예제 #2
0
 public DatadogSerilogLogger(ILogger logger, ILogRateLimiter rateLimiter)
 {
     _logger      = logger;
     _rateLimiter = rateLimiter;
 }