Skip to content

Commit

Permalink
Added aspnet-traceidentifier for AspNet for NLog.Web
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Dec 2, 2018
1 parent e40731f commit 8c1c6ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,38 @@ public class AspNetTraceIdentifierLayoutRenderer : AspNetLayoutRendererBase
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpContext = HttpContextAccessor.HttpContext;
builder.Append(httpContext.TraceIdentifier);
builder.Append(LookupTraceIdentifier(httpContext));
}

#if ASP_NET_CORE
private string LookupTraceIdentifier(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
return httpContext.TraceIdentifier;
}
#else
/// <summary>
/// Requires IIS ETW feature enabled. https://docs.microsoft.com/en-us/iis/configuration/system.webServer/httpTracing/
///
/// See also http://blog.tatham.oddie.com.au/2012/02/07/code-request-correlation-in-asp-net/
/// </summary>
private string LookupTraceIdentifier(System.Web.HttpContextBase httpContext)
{
var provider = httpContext as IServiceProvider;
if (provider != null)
{
var workerRequest = (System.Web.HttpWorkerRequest)provider.GetService(typeof(System.Web.HttpWorkerRequest));
if (workerRequest != null)
{
Guid requestIdGuid = workerRequest.RequestTraceIdentifier;
if (requestIdGuid != Guid.Empty)
{
return requestIdGuid.ToString();
}
}
}

return null;
}
#endif
}
}
3 changes: 3 additions & 0 deletions NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestIpLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetSessionIdLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetSessionValueLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetTraceIdentifierLayoutRenderer.cs">
<Link>AspNetTraceIdentifierLayoutRenderer.cs</Link>
</Compile>
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserAuthTypeLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserIdentityLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetWebRootPathLayoutRenderer.cs" />
Expand Down

0 comments on commit 8c1c6ce

Please sign in to comment.