Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
12b3643
Add NLog instrumentation for OpenTelemetry .NET Auto-Instrumentation
danifitz Aug 6, 2025
dd0eee6
Fix: https://github.com/open-telemetry/opentelemetry-dotnet-instrumen…
danifitz Aug 11, 2025
1b5ee77
Fix https://github.com/open-telemetry/opentelemetry-dotnet-instrument…
danifitz Aug 11, 2025
489fa3b
Fix: https://github.com/open-telemetry/opentelemetry-dotnet-instrumen…
danifitz Aug 11, 2025
638a6aa
Test: https://github.com/open-telemetry/opentelemetry-dotnet-instrume…
danifitz Aug 11, 2025
206be87
feat: refactor NLog instrumentation to use standard NLog Target archi…
danifitz Aug 20, 2025
799ef71
refactor: remove unused NLog.Extensions.Logging from TestApplication.…
danifitz Aug 20, 2025
4401757
refactor: optimize NLog target for async compatibility and performance
danifitz Aug 21, 2025
0fcf644
feat: add hybrid typed layout support for NLog version compatibility
danifitz Aug 27, 2025
4ffa40a
feat: implement NLog v5.3.4+ typed layouts for OpenTelemetryTarget
danifitz Sep 10, 2025
f6cd7d5
fix test coverage
danifitz Sep 10, 2025
665fb09
fix: correct NLog bridge EmitLog method call to match OpenTelemetry SDK
danifitz Sep 10, 2025
3a79c85
refactor: simplify NLog target configuration to use environment varia…
danifitz Sep 11, 2025
f6957e6
Removed NLog from AssemblyInfo
danifitz Sep 24, 2025
b078f85
feat: add NLog instrumentation with duck typing and NLog 6.x support
danifitz Sep 24, 2025
4a0d5a3
Merge branch 'main' into feature/nlog-instrumentation
danifitz Sep 24, 2025
bf571f5
fix CHANGELOG
Kielek Sep 30, 2025
16b004e
Fix build errors
Kielek Sep 30, 2025
e650142
commit generated file
Kielek Sep 30, 2025
c41f73c
Merge branch 'main' into feature/nlog-instrumentation
Kielek Sep 30, 2025
e66da6a
Move NLog version to test folder
Kielek Sep 30, 2025
3d5aff8
cleanup solution
Kielek Sep 30, 2025
ad1c62f
add NLOG to dictionary
Kielek Sep 30, 2025
54dd6da
typo fixes
Kielek Sep 30, 2025
0602818
remove reference to NLog.Extensions.Logging
Kielek Sep 30, 2025
903ab69
Update tested versions
Kielek Sep 30, 2025
4702a68
Minimal assembly version set to 4.0.0
Kielek Sep 30, 2025
951c355
fix sln file
Kielek Sep 30, 2025
29d4251
remove reference to System.Private.Uri
Kielek Sep 30, 2025
29bde76
Fix compilation for tests app
Kielek Sep 30, 2025
911fbd8
Apply suggestions from code review
lachmatt Oct 1, 2025
60311e1
Merge branch 'main' into feature/nlog-instrumentation
Kielek Oct 1, 2025
d912c7e
Merge branch 'main' into feature/nlog-instrumentation
Kielek Oct 1, 2025
c6b7bde
Merge branch 'main' into feature/nlog-instrumentation
Kielek Oct 2, 2025
fad8ea4
Fix issue occurring in VS
Kielek Oct 2, 2025
e3d9274
Add missing settings test case
Kielek Oct 2, 2025
e95e972
remove redundant lines
Kielek Oct 2, 2025
62f933d
Sync implementation with available documentation
Kielek Oct 2, 2025
33546ee
user facing documentation
Kielek Oct 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
<PackageVersion Include="log4net" Version="3.1.0" />
<PackageVersion Include="MinVer" Version="6.0.0" />
<PackageVersion Include="NLog" Version="5.3.2" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageVersion Include="OpenTelemetry" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Api" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
OpenTelemetry.AutoInstrumentation.Instrumentations.NLog.Bridge.Integrations.TargetCollectionIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.AsyncDefaultBasicConsumerIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.DefaultBasicConsumerIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.ModelBaseBasicGetIntegration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
OpenTelemetry.AutoInstrumentation.Instrumentations.NLog.Bridge.Integrations.TargetCollectionIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.AsyncDefaultBasicConsumerIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.DefaultBasicConsumerIntegration
OpenTelemetry.AutoInstrumentation.Instrumentations.RabbitMqLegacy.Integrations.ModelBaseBasicGetIntegration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ public static class Logs
/// </summary>
public const string EnableLog4NetBridge = "OTEL_DOTNET_AUTO_LOGS_ENABLE_LOG4NET_BRIDGE";

/// <summary>
/// Configuration key for whether or not experimental NLog bridge
/// should be enabled.
/// </summary>
public const string EnableNLogBridge = "OTEL_DOTNET_AUTO_LOGS_ENABLE_NLOG_BRIDGE";

/// <summary>
/// Configuration key for disabling all log instrumentations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ internal enum LogInstrumentation
/// Log4Net instrumentation.
/// </summary>
Log4Net = 1,

/// <summary>
/// NLog instrumentation.
/// </summary>
NLog = 2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ internal class LogSettings : Settings
/// </summary>
public bool EnableLog4NetBridge { get; private set; }

/// <summary>
/// Gets a value indicating whether the experimental NLog bridge is enabled.
/// </summary>
public bool EnableNLogBridge { get; private set; }

/// <summary>
/// Gets the list of enabled instrumentations.
/// </summary>
Expand All @@ -54,6 +59,7 @@ protected override void OnLoad(Configuration configuration)

IncludeFormattedMessage = configuration.GetBool(ConfigurationKeys.Logs.IncludeFormattedMessage) ?? false;
EnableLog4NetBridge = configuration.GetBool(ConfigurationKeys.Logs.EnableLog4NetBridge) ?? false;
EnableNLogBridge = configuration.GetBool(ConfigurationKeys.Logs.EnableNLogBridge) ?? false;

var instrumentationEnabledByDefault =
configuration.GetBool(ConfigurationKeys.Logs.LogsInstrumentationEnabled) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static partial class InstrumentationDefinitions
{
private static NativeCallTargetDefinition[] GetDefinitionsArray()
{
var nativeCallTargetDefinitions = new List<NativeCallTargetDefinition>(39);
var nativeCallTargetDefinitions = new List<NativeCallTargetDefinition>(40);
// Traces
var tracerSettings = Instrumentation.TracerSettings.Value;
if (tracerSettings.TracesEnabled)
Expand Down Expand Up @@ -104,6 +104,12 @@ private static NativeCallTargetDefinition[] GetDefinitionsArray()
{
nativeCallTargetDefinitions.Add(new("Microsoft.Extensions.Logging", "Microsoft.Extensions.Logging.LoggingBuilder", ".ctor", ["System.Void", "Microsoft.Extensions.DependencyInjection.IServiceCollection"], 9, 0, 0, 9, 65535, 65535, AssemblyFullName, "OpenTelemetry.AutoInstrumentation.Instrumentations.Logger.LoggingBuilderIntegration"));
}

// NLog
if (logSettings.EnabledInstrumentations.Contains(LogInstrumentation.NLog))
{
nativeCallTargetDefinitions.Add(new("NLog", "NLog.Config.LoggingConfiguration", "GetConfiguredNamedTargets", ["NLog.Targets.Target[]"], 4, 0, 0, 6, 65535, 65535, AssemblyFullName, "OpenTelemetry.AutoInstrumentation.Instrumentations.NLog.Bridge.Integrations.TargetCollectionIntegration"));
}
}

// Metrics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.AutoInstrumentation.CallTarget;
using OpenTelemetry.AutoInstrumentation.Logging;
#if NET
using OpenTelemetry.AutoInstrumentation.Logger;
#endif

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.NLog.Bridge.Integrations;

/// <summary>
/// NLog Target Collection integration.
/// This integration hooks into NLog's target collection methods to automatically
/// inject the OpenTelemetry target when the NLog bridge is enabled.
///
/// The integration targets NLog's LoggingConfiguration.GetConfiguredNamedTargets method
/// which is called when NLog retrieves the list of configured targets.
/// </summary>
[InstrumentMethod(
assemblyName: "NLog",
typeName: "NLog.Config.LoggingConfiguration",
methodName: "GetConfiguredNamedTargets",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find method with this name in any recent version of NLog.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I made an incorrect assumption when following some of the work in Log4Net bridge instrumentation. I had to make quite a few changes in #1b5ee77

returnTypeName: "NLog.Targets.Target[]",
parameterTypeNames: new string[0],
minimumVersion: "4.0.0",
maximumVersion: "6.*.*",
integrationName: "NLog",
type: InstrumentationType.Log)]
public static class TargetCollectionIntegration
{
#if NET
private static readonly IOtelLogger Logger = OtelLogging.GetLogger();
private static int _warningLogged;
#endif

/// <summary>
/// Intercepts the completion of NLog's GetConfiguredNamedTargets method.
/// This method is called after NLog has retrieved its configured targets,
/// allowing us to inject the OpenTelemetry target into the collection.
/// </summary>
/// <typeparam name="TTarget">The type of the target being returned.</typeparam>
/// <typeparam name="TReturn">The type of the return value (target array).</typeparam>
/// <param name="instance">The LoggingConfiguration instance.</param>
/// <param name="returnValue">The array of configured targets returned by NLog.</param>
/// <param name="exception">Any exception that occurred during the method execution.</param>
/// <param name="state">The call target state.</param>
/// <returns>A CallTargetReturn containing the modified target array with the OpenTelemetry target injected.</returns>
internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, in CallTargetState state)
{
#if NET
// Check if ILogger bridge has been initialized and warn if so
// This prevents conflicts between different logging bridges
if (LoggerInitializer.IsInitializedAtLeastOnce)
{
if (Interlocked.Exchange(ref _warningLogged, 1) != default)
{
return new CallTargetReturn<TReturn>(returnValue);
}

Logger.Warning("Disabling addition of NLog bridge due to ILogger bridge initialization.");
return new CallTargetReturn<TReturn>(returnValue);
}
#endif

// Only inject the target if the NLog bridge is enabled and we have a valid target array
if (Instrumentation.LogSettings.Value.EnableNLogBridge && returnValue is Array targetsArray)
{
// Use the target initializer to inject the OpenTelemetry target
var modifiedTargets = OpenTelemetryTargetInitializer<TReturn>.Initialize(targetsArray);
return new CallTargetReturn<TReturn>(modifiedTargets);
}

// Return the original targets if injection is not enabled or not applicable
return new CallTargetReturn<TReturn>(returnValue);
}
}
Loading