|
| 1 | +// <copyright file="Logger.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +#nullable enable |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Globalization; |
| 10 | +using System.IO; |
| 11 | +using Datadog.Trace.Logging; |
| 12 | +using Datadog.Trace.Logging.Internal.Configuration; |
| 13 | +using Datadog.Trace.Util; |
| 14 | + |
| 15 | +namespace Datadog.Trace.LibDatadog; |
| 16 | + |
| 17 | +internal sealed class Logger |
| 18 | +{ |
| 19 | + private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<Logger>(); |
| 20 | + |
| 21 | + private bool _loggingEnabled = false; |
| 22 | + |
| 23 | + public static Logger Instance { get; } = new(); |
| 24 | + |
| 25 | + public void Enable(FileLoggingConfiguration fileConfig, DomainMetadata domainMetadata) |
| 26 | + { |
| 27 | + // Rebuild the logger even if it has already been enabled, in case any configuration has changed |
| 28 | + |
| 29 | + var filePath = Path.Combine( |
| 30 | + fileConfig.LogDirectory, |
| 31 | + $"dotnet-tracer-libdatadog-{domainMetadata.ProcessName}-{domainMetadata.ProcessId.ToString(CultureInfo.InvariantCulture)}.log"); |
| 32 | + |
| 33 | + using var path = new CharSlice(filePath); |
| 34 | + var cfg = new FileConfig |
| 35 | + { |
| 36 | + Path = path, |
| 37 | + MaxFiles = 31, // Matches existing Serilog configuration |
| 38 | + MaxSizeBytes = (ulong)fileConfig.MaxLogFileSizeBytes, |
| 39 | + }; |
| 40 | + |
| 41 | + try |
| 42 | + { |
| 43 | + Log.Debug("Enabling libdatadog logger"); |
| 44 | + using var error = NativeInterop.Logger.ConfigureFile(cfg); |
| 45 | + error.ThrowIfError(); |
| 46 | + _loggingEnabled = true; |
| 47 | + } |
| 48 | + catch (Exception ex) |
| 49 | + { |
| 50 | + _loggingEnabled = false; |
| 51 | + Log.Error(ex, "Failed to configure libdatadog logger"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void Disable() |
| 56 | + { |
| 57 | + if (!_loggingEnabled) |
| 58 | + { |
| 59 | + Log.Debug("Libdatadog logging is not enabled, skipping disable operation."); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + Log.Debug("Disabling libdatadog logger"); |
| 66 | + using var error = NativeInterop.Logger.DisableFile(); |
| 67 | + error.ThrowIfError(); |
| 68 | + _loggingEnabled = false; |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + Log.Error(ex, "Failed to disable libdatadog logger"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void SetLogLevel(bool debugEnabled) |
| 77 | + { |
| 78 | + var logLevel = debugEnabled ? Vendors.Serilog.Events.LogEventLevel.Debug : DatadogLogging.DefaultLogLevel; |
| 79 | + SetLogLevel(logLevel); |
| 80 | + } |
| 81 | + |
| 82 | + private void SetLogLevel(Vendors.Serilog.Events.LogEventLevel logLevel) |
| 83 | + { |
| 84 | + if (!_loggingEnabled) |
| 85 | + { |
| 86 | + // This could happen if, for example, datapipeline is enabled, and we trigger a tracer flare to change the log level. |
| 87 | + Log.Information("Attempted to set libdatadog log level to {LogLevel} without enabling logging first", logLevel); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + var level = logLevel.ToLogEventLevel(); |
| 92 | + try |
| 93 | + { |
| 94 | + using var error = NativeInterop.Logger.SetLogLevel(level); |
| 95 | + error.ThrowIfError(); |
| 96 | + } |
| 97 | + catch (Exception ex) |
| 98 | + { |
| 99 | + Log.Error(ex, "Failed to set libdatadog log level"); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments