From 180e96c9fccc011c01514be16b36e7c3dbf53d9b Mon Sep 17 00:00:00 2001 From: NachoEchevarria Date: Tue, 21 Apr 2026 11:30:12 +0200 Subject: [PATCH 1/3] Fix exception --- tracer/missing-nullability-files.csv | 1 - .../AppenderAttachedImplIntegration.cs | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tracer/missing-nullability-files.csv b/tracer/missing-nullability-files.csv index 64b040254928..a1b2f4a03ce9 100644 --- a/tracer/missing-nullability-files.csv +++ b/tracer/missing-nullability-files.csv @@ -585,7 +585,6 @@ src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/DirectSubmissi src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/DirectSubmission/ILoggingEventLegacyDuck.cs src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/DirectSubmission/LevelDuck.cs src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/DirectSubmission/LevelDuckExtensions.cs -src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/ILoggingEvent.cs src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/NLog/DirectSubmission/NLogConstants.cs src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/NLog/LogsInjection/DiagnosticContextHelper.cs diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs index fcb837269b32..fa7f6191fe86 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs @@ -3,6 +3,8 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + using System; using System.ComponentModel; using Datadog.Trace.ClrProfiler.AutoInstrumentation.Logging; @@ -34,7 +36,7 @@ public sealed class AppenderAttachedImplIntegration /// Instance value, aka `this` of the instrumented method. /// The logging event /// Calltarget state value - internal static CallTargetState OnMethodBegin(TTarget instance, TLoggingEvent loggingEvent) + internal static CallTargetState OnMethodBegin(TTarget instance, TLoggingEvent? loggingEvent) where TLoggingEvent : ILoggingEvent { if (loggingEvent?.Instance is null) @@ -44,11 +46,16 @@ internal static CallTargetState OnMethodBegin(TTarget in var tracer = Tracer.Instance; - var mutableSettings = tracer.CurrentTraceSettings.Settings; + var mutableSettings = tracer.CurrentTraceSettings?.Settings; + if (mutableSettings is null || !mutableSettings.LogsInjectionEnabled) + { + return CallTargetState.GetDefault(); + } + var properties = loggingEvent.Properties; - if (mutableSettings.LogsInjectionEnabled && properties != null && !properties.Contains(CorrelationIdentifier.ServiceKey)) + if (properties is not null && !properties.Contains(CorrelationIdentifier.ServiceKey)) { - properties[CorrelationIdentifier.ServiceKey] = mutableSettings.DefaultServiceName; + properties[CorrelationIdentifier.ServiceKey] = mutableSettings.DefaultServiceName ?? string.Empty; properties[CorrelationIdentifier.VersionKey] = mutableSettings.ServiceVersion ?? string.Empty; properties[CorrelationIdentifier.EnvKey] = mutableSettings.Environment ?? string.Empty; @@ -60,7 +67,7 @@ internal static CallTargetState OnMethodBegin(TTarget in } } - return new CallTargetState(scope: null, state: null); + return CallTargetState.GetDefault(); } /// @@ -73,9 +80,9 @@ internal static CallTargetState OnMethodBegin(TTarget in /// Exception instance in case the original code threw an exception. /// Calltarget state value /// A response value, in an async scenario will be T of Task of T - internal static CallTargetReturn OnMethodEnd(TTarget instance, TReturn returnValue, Exception exception, in CallTargetState state) + internal static CallTargetReturn OnMethodEnd(TTarget instance, TReturn? returnValue, Exception? exception, in CallTargetState state) { - return new CallTargetReturn(returnValue); + return new CallTargetReturn(returnValue); } } } From 6034d5dfbd0df13a4a0363e6eb9fc65d6116edf0 Mon Sep 17 00:00:00 2001 From: NachoEchevarria Date: Tue, 21 Apr 2026 13:22:24 +0200 Subject: [PATCH 2/3] Log. Nits --- .../AppenderAttachedImplIntegration.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs index fa7f6191fe86..dad85e893a4e 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs @@ -9,6 +9,7 @@ using System.ComponentModel; using Datadog.Trace.ClrProfiler.AutoInstrumentation.Logging; using Datadog.Trace.ClrProfiler.CallTarget; +using Datadog.Trace.Logging; namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Log4Net { @@ -28,6 +29,9 @@ namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Log4Net [EditorBrowsable(EditorBrowsableState.Never)] public sealed class AppenderAttachedImplIntegration { + private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(AppenderAttachedImplIntegration)); + private static bool _loggedNullSettings; + /// /// OnMethodBegin callback /// @@ -47,7 +51,18 @@ internal static CallTargetState OnMethodBegin(TTarget in var tracer = Tracer.Instance; var mutableSettings = tracer.CurrentTraceSettings?.Settings; - if (mutableSettings is null || !mutableSettings.LogsInjectionEnabled) + if (mutableSettings is null) + { + if (!_loggedNullSettings) + { + _loggedNullSettings = true; + Log.Debug("log4net logs-injection skipped: CurrentTraceSettings was null."); + } + + return CallTargetState.GetDefault(); + } + + if (!mutableSettings.LogsInjectionEnabled) { return CallTargetState.GetDefault(); } @@ -55,7 +70,7 @@ internal static CallTargetState OnMethodBegin(TTarget in var properties = loggingEvent.Properties; if (properties is not null && !properties.Contains(CorrelationIdentifier.ServiceKey)) { - properties[CorrelationIdentifier.ServiceKey] = mutableSettings.DefaultServiceName ?? string.Empty; + properties[CorrelationIdentifier.ServiceKey] = mutableSettings.DefaultServiceName; properties[CorrelationIdentifier.VersionKey] = mutableSettings.ServiceVersion ?? string.Empty; properties[CorrelationIdentifier.EnvKey] = mutableSettings.Environment ?? string.Empty; From b1b28de5584dc77bc48952f2c7dc6498ddadb5ec Mon Sep 17 00:00:00 2001 From: NachoEchevarria Date: Wed, 22 Apr 2026 17:14:35 +0200 Subject: [PATCH 3/3] loggingEvent cannot be null --- .../LogsInjection/AppenderAttachedImplIntegration.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs index dad85e893a4e..b850322af03d 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/Log4Net/LogsInjection/AppenderAttachedImplIntegration.cs @@ -40,7 +40,7 @@ public sealed class AppenderAttachedImplIntegration /// Instance value, aka `this` of the instrumented method. /// The logging event /// Calltarget state value - internal static CallTargetState OnMethodBegin(TTarget instance, TLoggingEvent? loggingEvent) + internal static CallTargetState OnMethodBegin(TTarget instance, TLoggingEvent loggingEvent) where TLoggingEvent : ILoggingEvent { if (loggingEvent?.Instance is null) @@ -95,9 +95,9 @@ internal static CallTargetState OnMethodBegin(TTarget in /// Exception instance in case the original code threw an exception. /// Calltarget state value /// A response value, in an async scenario will be T of Task of T - internal static CallTargetReturn OnMethodEnd(TTarget instance, TReturn? returnValue, Exception? exception, in CallTargetState state) + internal static CallTargetReturn OnMethodEnd(TTarget instance, TReturn returnValue, Exception? exception, in CallTargetState state) { - return new CallTargetReturn(returnValue); + return new CallTargetReturn(returnValue); } } }