-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathAppenderAttachedImplIntegration.cs
More file actions
103 lines (91 loc) · 4.49 KB
/
Copy pathAppenderAttachedImplIntegration.cs
File metadata and controls
103 lines (91 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// <copyright file="AppenderAttachedImplIntegration.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#nullable enable
using System;
using System.ComponentModel;
using Datadog.Trace.ClrProfiler.AutoInstrumentation.Logging;
using Datadog.Trace.ClrProfiler.CallTarget;
using Datadog.Trace.Logging;
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Log4Net
{
/// <summary>
/// LoggerFactoryScopeProvider.ForEach<TState> calltarget instrumentation
/// </summary>
[InstrumentMethod(
AssemblyName = "log4net",
TypeName = "log4net.Util.AppenderAttachedImpl",
MethodName = "AppendLoopOnAppenders",
ReturnTypeName = ClrNames.Int32,
ParameterTypeNames = new[] { "log4net.Core.LoggingEvent" },
MinimumVersion = "1.0.0",
MaximumVersion = "3.*.*",
IntegrationName = "Log4Net")]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class AppenderAttachedImplIntegration
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(AppenderAttachedImplIntegration));
private static bool _loggedNullSettings;
/// <summary>
/// OnMethodBegin callback
/// </summary>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <typeparam name="TLoggingEvent">The type of the logging event</typeparam>
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
/// <param name="loggingEvent">The logging event</param>
/// <returns>Calltarget state value</returns>
internal static CallTargetState OnMethodBegin<TTarget, TLoggingEvent>(TTarget instance, TLoggingEvent loggingEvent)
where TLoggingEvent : ILoggingEvent
{
if (loggingEvent?.Instance is null)
{
return CallTargetState.GetDefault();
}
var tracer = Tracer.Instance;
var mutableSettings = tracer.CurrentTraceSettings?.Settings;
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();
}
var properties = loggingEvent.Properties;
if (properties is not null && !properties.Contains(CorrelationIdentifier.ServiceKey))
{
properties[CorrelationIdentifier.ServiceKey] = mutableSettings.DefaultServiceName;
properties[CorrelationIdentifier.VersionKey] = mutableSettings.ServiceVersion ?? string.Empty;
properties[CorrelationIdentifier.EnvKey] = mutableSettings.Environment ?? string.Empty;
if (tracer.DistributedSpanContext is { } context &&
LogContext.TryGetValues(context, out var traceId, out var spanId, tracer.Settings.TraceId128BitLoggingEnabled))
{
properties[CorrelationIdentifier.TraceIdKey] = traceId;
properties[CorrelationIdentifier.SpanIdKey] = spanId;
}
}
return CallTargetState.GetDefault();
}
/// <summary>
/// OnMethodEnd callback
/// </summary>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <typeparam name="TReturn">Type of the return value</typeparam>
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
/// <param name="returnValue">Original return value</param>
/// <param name="exception">Exception instance in case the original code threw an exception.</param>
/// <param name="state">Calltarget state value</param>
/// <returns>A response value, in an async scenario will be T of Task of T</returns>
internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception? exception, in CallTargetState state)
{
return new CallTargetReturn<TReturn>(returnValue);
}
}
}