|
using System.Threading.Tasks; |
|
using Microsoft.AspNetCore.Http; |
|
using NLog.Common; |
|
using NLog.Web.LayoutRenderers; |
|
#if ASP_NET_CORE3 |
|
using Microsoft.AspNetCore.Components; |
|
#endif |
|
|
|
namespace NLog.Web |
|
{ |
|
/// <summary> |
|
/// This class is to intercept the HTTP pipeline and to allow additional logging of the following |
|
/// |
|
/// Navigation Manager |
|
/// |
|
/// Usage: app.UseMiddleware<NLogNavigationManagerMiddleware>(); where app is an IApplicationBuilder |
|
/// |
|
/// Inject the NLogNavigationManagerMiddlewareOptions in the IoC if wanting to override default values for constructor |
|
/// </summary> |
|
public class NLogNavigationManagerMiddleware |
|
{ |
|
private readonly RequestDelegate _next; |
|
private readonly NLogNavigationManagerMiddlewareOptions _options; |
|
|
|
/// <summary> |
|
/// Initializes new instance of the <see cref="NLogNavigationManagerMiddleware"/> class |
|
/// </summary> |
|
/// <remarks> |
|
/// Use the following in Startup.cs: |
|
/// <code> |
|
/// public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|
/// { |
|
/// app.UseMiddleware<NLog.Web.NLogNavigationManagerMiddleware>(); |
|
/// } |
|
/// </code> |
|
/// </remarks> |
|
public NLogNavigationManagerMiddleware(RequestDelegate next, NLogNavigationManagerMiddlewareOptions options = default) |
|
{ |
|
_next = next; |
|
_options = options ?? NLogNavigationManagerMiddlewareOptions.Default; |
|
} |
|
|
|
#if ASP_NET_CORE3 |
|
/// <summary> |
|
/// This allows interception of the HTTP pipeline for querying and managing URI navigation |
|
/// </summary> |
|
/// <param name="context">The HttpContext</param> |
|
/// <param name="navigationManager">The NavigationManager</param> |
|
/// <returns></returns> |
|
public async Task Invoke(HttpContext context, NavigationManager navigationManager) |
|
{ |
|
if (ShouldCaptureNavigationManager(context)) |
|
{ |
|
var requestUri = string.Empty; |
|
try |
|
{ |
|
requestUri = navigationManager.Uri; |
|
} |
|
catch |
|
{ |
|
InternalLogger.Debug("NLogNavigationManagerMiddleware: NavigationManager not initialized"); |
|
} |
|
|
|
if (!string.IsNullOrEmpty(requestUri)) |
|
{ |
|
context.Items[AspNetRequestPostedBodyLayoutRenderer.NLogPostedRequestBodyKey] = requestUri; |
|
} |
|
} |
|
|
|
// Execute the next class in the HTTP pipeline, this can be the next middleware or the actual handler |
|
await _next(context).ConfigureAwait(false); |
|
} |
|
#else |
|
/// <summary> |
|
/// This allows interception of the HTTP pipeline for querying and managing URI navigation |
|
/// </summary> |
|
/// /// <param name="context">The HttpContext</param> |
|
/// <returns></returns> |
|
public async Task Invoke(HttpContext context) |
|
{ |
|
InternalLogger.Debug("NLogNavigationManagerMiddleware: ASP.NET Core version not supported, skipping middleware execution"); |
|
// Execute the next class in the HTTP pipeline, this can be the next middleware or the actual handler |
|
await _next(context).ConfigureAwait(false); |
|
} |
|
#endif |
|
|
|
private bool ShouldCaptureNavigationManager(HttpContext context) |
|
{ |
|
// Perform null checking |
|
if (context == null) |
|
{ |
|
InternalLogger.Debug("NLogNavigationManagerMiddleware: HttpContext is null"); |
|
return false; |
|
} |
|
|
|
return _options.ShouldCapture(context); |
|
} |
|
} |
|
} |