Skip to content

Commit

Permalink
fix #79 only inject layout renderer keys for APM if Elastic.Apm.NLog … (
Browse files Browse the repository at this point in the history
#83)

* fix #79 only inject layout renderer keys for APM if Elastic.Apm.NLog is referenced

* move logic to class instantation
  • Loading branch information
Mpdreamz authored and russcam committed Jun 1, 2021
1 parent b3e0f7f commit cbea7b0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using NLog;
using NLog.Config;
Expand All @@ -21,6 +23,15 @@ public class EcsLayout : Layout
{
public const string Name = nameof(EcsLayout);

private static bool? _nlogApmLoaded;

private static bool NLogApmLoaded()
{
if (_nlogApmLoaded.HasValue) return _nlogApmLoaded.Value;
_nlogApmLoaded = Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null;
return _nlogApmLoaded.Value;
}

private readonly Layout _disableThreadAgnostic = "${threadid:cached=true}";

public EcsLayout()
Expand All @@ -42,8 +53,11 @@ public EcsLayout()
ServerUser = "${environment-user}"; // NLog 4.6.4

// These values are set by the Elastic.Apm.NLog package
ApmTraceId = "${ElasticApmTraceId}";
ApmTransactionId = "${ElasticApmTransactionId}";
if (NLogApmLoaded())
{
ApmTraceId = "${ElasticApmTraceId}";
ApmTransactionId = "${ElasticApmTransactionId}";
}
}

public Layout AgentId { get; set; }
Expand Down

0 comments on commit cbea7b0

Please sign in to comment.