Skip to content

Commit 6d1f263

Browse files
committed
Protect against FileLoadException for NLog Assembly
1 parent e4d80e4 commit 6d1f263

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/NLog/LogsInjection/LogsInjectionHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ internal static class LogsInjectionHelper<TTarget>
2727

2828
static LogsInjectionHelper()
2929
{
30+
// Use typeof(TTarget).Assembly to get the NLog assembly safely, avoiding Type.GetType()
31+
// which can throw FileLoadException even with throwOnError: false when there are
32+
// assembly loading issues (version mismatches, binding redirects, etc.)
33+
var nlogAssembly = typeof(TTarget).Assembly;
34+
3035
// this is not available in older versions of NLog (e.g., v2.1 doesn't have JSON support)
31-
_jsonAttributeType = Type.GetType("NLog.Layouts.JsonAttribute, NLog", throwOnError: false);
36+
_jsonAttributeType = nlogAssembly.GetType("NLog.Layouts.JsonAttribute", throwOnError: false);
3237
if (_jsonAttributeType is null)
3338
{
3439
return;
3540
}
3641

3742
// this simple layout should exist for all versions
38-
_simpleLayoutType = Type.GetType("NLog.Layouts.SimpleLayout, NLog", throwOnError: false);
43+
_simpleLayoutType = nlogAssembly.GetType("NLog.Layouts.SimpleLayout", throwOnError: false);
3944
}
4045

4146
/// <summary>

0 commit comments

Comments
 (0)