-
Notifications
You must be signed in to change notification settings - Fork 166
Log4Net guard AppenderAttachedImpl integration against NullReferenceException. #8489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,13 @@ | |
| // 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 | ||
| { | ||
|
|
@@ -26,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; | ||
|
|
||
| /// <summary> | ||
| /// OnMethodBegin callback | ||
| /// </summary> | ||
|
|
@@ -44,9 +50,25 @@ internal static CallTargetState OnMethodBegin<TTarget, TLoggingEvent>(TTarget in | |
|
|
||
| var tracer = Tracer.Instance; | ||
|
|
||
| var mutableSettings = tracer.CurrentTraceSettings.Settings; | ||
| var mutableSettings = tracer.CurrentTraceSettings?.Settings; | ||
| if (mutableSettings is null) | ||
| { | ||
| if (!_loggedNullSettings) | ||
| { | ||
| _loggedNullSettings = true; | ||
| Log.Debug("log4net logs-injection skipped: CurrentTraceSettings was null."); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsure how valuable this is, if the theory here is correct there will only be an occasional missed injection
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (at least as I understand it) |
||
| } | ||
|
|
||
| return CallTargetState.GetDefault(); | ||
| } | ||
|
|
||
| if (!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.VersionKey] = mutableSettings.ServiceVersion ?? string.Empty; | ||
|
|
@@ -60,7 +82,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TLoggingEvent>(TTarget in | |
| } | ||
| } | ||
|
|
||
| return new CallTargetState(scope: null, state: null); | ||
| return CallTargetState.GetDefault(); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -73,7 +95,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TLoggingEvent>(TTarget in | |
| /// <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) | ||
| internal static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception? exception, in CallTargetState state) | ||
| { | ||
| return new CallTargetReturn<TReturn>(returnValue); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is the case if we'd need to guard this elsewhere for other instrumentations, if so something that we can do later