Skip to content

Commit

Permalink
Changed to Lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Feb 8, 2025
1 parent 0e99ec9 commit b3b3c65
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,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 @@ -88,7 +88,7 @@ protected override void InitializeLayout()
{
if (CanIncludeAspNetProperties())
{
if (NLogWeb5Registered())
if (NLogWeb5Registered.Value)
EventDurationMs = "${aspnet-request-duration}";

HttpRequestId = "${aspnet-TraceIdentifier}";
Expand All @@ -104,25 +104,25 @@ 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 NLogApmLoaded() => Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null;
private static Lazy<bool> NLogApmLoaded { get; } = new Lazy<bool>(() => Type.GetType("Elastic.Apm.NLog.ApmTraceIdLayoutRenderer, Elastic.Apm.NLog") != null);

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

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

/// <summary></summary>
Expand Down Expand Up @@ -216,7 +216,7 @@ protected override void InitializeLayout()
/// <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

0 comments on commit b3b3c65

Please sign in to comment.