Skip to content
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

NLog EcsLayout stop using obsolete ConfigurationItemFactory members #478

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,13 @@ public class EcsLayout : Layout
{
/// <summary> An NLOG layout implementation that renders logs as ECS json</summary>
public const string Name = nameof(EcsLayout);
private static bool? _nlogApmLoaded;
private static Agent _defaultAgent;
private Agent _previousAgent;
private Service _previousService;
private Host _previousHost;
private Server _previousServer;
private Process _previousProcess;

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}";

/// <summary> An NLOG layout implementation that renders logs as ECS json</summary>
Expand Down Expand Up @@ -76,7 +68,7 @@ public EcsLayout()
_defaultAgent = EcsDocument.CreateAgent(typeof(EcsLayout));

// These values are set by the Elastic.Apm.NLog package
if (NLogApmLoaded())
if (NLogApmLoaded.Value)
{
ApmTraceId = "${ElasticApmTraceId}";
ApmTransactionId = "${ElasticApmTransactionId}";
Expand All @@ -96,7 +88,7 @@ protected override void InitializeLayout()
{
if (CanIncludeAspNetProperties())
{
if (NLogWeb5Registered())
if (NLogWeb5Registered.Value)
EventDurationMs = "${aspnet-request-duration}";

HttpRequestId = "${aspnet-TraceIdentifier}";
Expand All @@ -112,18 +104,26 @@ protected override void InitializeLayout()
UrlQuery = "${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=false:IncludeQueryString=true}";
UrlUserName = "${aspnet-user-identity}";

if (!NLogApmLoaded())
if (!NLogApmLoaded.Value)
ApmTraceId = "${scopeproperty:item=RequestId:whenEmpty=${aspnet-TraceIdentifier}}";
}

base.InitializeLayout();
}

private static bool NLogWeb5Registered() =>
ConfigurationItemFactory.Default.LayoutRenderers.TryGetDefinition("aspnet-request-duration", out _);
private static Lazy<bool> NLogApmLoaded { get; } = new Lazy<bool>(() => Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null);

#if NETFRAMEWORK
private static Lazy<bool> NLogWeb4Registered { get; } = new Lazy<bool>(() => Type.GetType("NLog.Web.LayoutRenderers.AspNetRequestCookieLayoutRenderer, NLog.Web") != null);
#else
private static Lazy<bool> NLogWeb4Registered { get; } = new Lazy<bool>(() => Type.GetType("NLog.Web.LayoutRenderers.AspNetRequestCookieLayoutRenderer, NLog.Web.AspNetCore") != null);
#endif

private static bool NLogWeb4Registered() =>
ConfigurationItemFactory.Default.LayoutRenderers.TryGetDefinition("aspnet-request-url", out _);
#if NETFRAMEWORK
private static Lazy<bool> NLogWeb5Registered { get; } = new Lazy<bool>(() => Type.GetType("NLog.Web.LayoutRenderers.AspNetRequestDurationLayoutRenderer, NLog.Web") != null);
#else
private static Lazy<bool> NLogWeb5Registered { get; } = new Lazy<bool>(() => Type.GetType("NLog.Web.LayoutRenderers.AspNetRequestDurationLayoutRenderer, NLog.Web.AspNetCore") != null);
#endif

/// <summary></summary>
// ReSharper disable UnusedMember.Global
Expand Down Expand Up @@ -216,7 +216,7 @@ private static bool NLogWeb4Registered() =>
/// <summary>
/// Tests if aspnet properties would be rendered
/// </summary>
public bool CanIncludeAspNetProperties() => IncludeAspNetProperties && NLogWeb4Registered();
public bool CanIncludeAspNetProperties() => IncludeAspNetProperties && NLogWeb4Registered.Value;

/// <summary></summary>
[ArrayParameter(typeof(TargetPropertyWithContext), "label")]
Expand Down
Loading