Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
NLog MDLC + NDLC when NLog ver. 4.6 is ready
  • Loading branch information
snakefoot committed Sep 6, 2018
commit 8b6ab9a5d386545c45e70e71602f6eb2e6a71a0c
2 changes: 1 addition & 1 deletion src/LibLog.Example.Library/LibLog.Example.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<DefineConstants>TRACE;DEBUG;LIBLOG_PUBLIC</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibLog" Version="5.0.0-build.421" />
<PackageReference Include="LibLog" Version="5.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<DefineConstants>TRACE;LIBLOG_PROVIDERS_ONLY</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibLog" Version="5.0.0-build.421" />
<PackageReference Include="LibLog" Version="5.0.0" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions src/LibLog.Tests/LogProviders/NLogLogProviderLoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public void Can_open_mapped_diagnostics_context()
}
}

#if NLOG4
[Fact]
public void Can_capture_callsite()
{
Expand All @@ -154,7 +153,6 @@ public void Can_capture_callsite()
_sut.Info(() => "c");
myTarget.Logs[myTarget.Logs.Count - 1].ShouldBe(string.Format("INFO|{0}.{1}|c|", GetType().FullName, nameof(Can_capture_callsite)));
}
#endif

[Fact]
public void Can_open_mapped_diagnostics_context_destructured()
Expand Down
41 changes: 37 additions & 4 deletions src/LibLog/LogProviders/NLogLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,55 @@ public static bool IsLoggerAvailable()

protected override OpenNdc GetOpenNdcMethod()
{
var messageParam = Expression.Parameter(typeof(string), "message");

var ndlcContextType = Type.GetType("NLog.NestedDiagnosticsLogicalContext, NLog");
if (ndlcContextType != null)
{
var pushObjectMethod = ndlcContextType.GetMethod("PushObject", typeof(object));
if (pushObjectMethod != null)
{
var pushObjectMethodCall = Expression.Call(null, pushObjectMethod, messageParam);
return Expression.Lambda<OpenNdc>(pushObjectMethodCall, messageParam).Compile();
}
}

var ndcContextType = Type.GetType("NLog.NestedDiagnosticsContext, NLog");
var pushMethod = ndcContextType.GetMethod("Push", typeof(string));
var messageParam = Expression.Parameter(typeof(string), "message");

var pushMethodCall = Expression.Call(null, pushMethod, messageParam);
return Expression.Lambda<OpenNdc>(pushMethodCall, messageParam).Compile();
}

protected override OpenMdc GetOpenMdcMethod()
{
var mdcContextType = Type.GetType("NLog.MappedDiagnosticsContext, NLog");
var keyParam = Expression.Parameter(typeof(string), "key");

var ndlcContextType = Type.GetType("NLog.NestedDiagnosticsLogicalContext, NLog");
if (ndlcContextType != null)
{
var pushObjectMethod = ndlcContextType.GetMethod("PushObject", typeof(object));
if (pushObjectMethod != null)
{
var mdlcContextType = Type.GetType("NLog.MappedDiagnosticsLogicalContext, NLog");
if (mdlcContextType != null)
{
var setScopedMethod = mdlcContextType.GetMethod("SetScoped", typeof(string), typeof(object));
if (setScopedMethod != null)
{
var valueObjParam = Expression.Parameter(typeof(object), "value");
var setScopedMethodCall = Expression.Call(null, setScopedMethod, keyParam, valueObjParam);
var setMethodLambda = Expression.Lambda<Func<string, object, IDisposable>>(setScopedMethodCall, keyParam, valueObjParam).Compile();
return (key, value, _) => setMethodLambda(key, value);
}
}
}
}

var mdcContextType = Type.GetType("NLog.MappedDiagnosticsContext, NLog");
var setMethod = mdcContextType.GetMethod("Set", typeof(string), typeof(string));
var removeMethod = mdcContextType.GetMethod("Remove", typeof(string));
var keyParam = Expression.Parameter(typeof(string), "key");
var valueParam = Expression.Parameter(typeof(string), "value");

var setMethodCall = Expression.Call(null, setMethod, keyParam, valueParam);
var removeMethodCall = Expression.Call(null, removeMethod, keyParam);

Expand Down