From 2c4fe33b5eb740f2670a01ce29e6f0a3286d8b6f Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Fri, 26 May 2023 20:06:44 +0200 Subject: [PATCH] AspNetBufferingTargetWrapper - Register for initial request, when loading NLog config --- .../ASP.NET 4.6.1 - VS2017.csproj | 42 +++++++++---------- .../ASP.NET 4.6.1 - VS2017/NLog.config | 2 +- .../ASP.NET 4.6.1 - VS2017/Web.config | 4 +- .../ASP.NET 4.6.1 - VS2017/packages.config | 6 +-- .../Wrappers/AspNetBufferingTargetWrapper.cs | 9 +++- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/ASP.NET 4.6.1 - VS2017.csproj b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/ASP.NET 4.6.1 - VS2017.csproj index a1fc3350..8bca3390 100644 --- a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/ASP.NET 4.6.1 - VS2017.csproj +++ b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/ASP.NET 4.6.1 - VS2017.csproj @@ -49,50 +49,48 @@ + + + - - - + + - + + + + - - - - - True - ..\..\..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll - - - - - - ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.1.2\lib\net45\System.Web.Helpers.dll + ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.Helpers.dll - - ..\..\..\..\packages\Microsoft.AspNet.Mvc.5.1.3\lib\net45\System.Web.Mvc.dll + + ..\..\..\..\packages\Microsoft.AspNet.Mvc.5.2.9\lib\net45\System.Web.Mvc.dll - ..\..\..\..\packages\Microsoft.AspNet.Razor.3.1.2\lib\net45\System.Web.Razor.dll + ..\..\..\..\packages\Microsoft.AspNet.Razor.3.2.9\lib\net45\System.Web.Razor.dll - ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.1.2\lib\net45\System.Web.WebPages.dll + ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.dll - ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.1.2\lib\net45\System.Web.WebPages.Deployment.dll + ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.Deployment.dll - ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.1.2\lib\net45\System.Web.WebPages.Razor.dll + ..\..\..\..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.Razor.dll + + + True + ..\..\..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll ..\..\..\..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll diff --git a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/NLog.config b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/NLog.config index f452e55c..5d3f50e5 100644 --- a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/NLog.config +++ b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/NLog.config @@ -15,7 +15,7 @@ + layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> diff --git a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/Web.config b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/Web.config index daf9a0ce..9fa3cbc3 100644 --- a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/Web.config +++ b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/Web.config @@ -14,6 +14,7 @@ + @@ -44,7 +45,7 @@ - + @@ -52,6 +53,7 @@ + diff --git a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config index 041532db..41ccc6f3 100644 --- a/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config +++ b/examples/ASP.NET 4.6.1/Visual Studio 2017/ASP.NET 4.6.1 - VS2017/packages.config @@ -4,11 +4,11 @@ - - + + - + diff --git a/src/Shared/Targets/Wrappers/AspNetBufferingTargetWrapper.cs b/src/Shared/Targets/Wrappers/AspNetBufferingTargetWrapper.cs index 660eb10f..4c8d923e 100644 --- a/src/Shared/Targets/Wrappers/AspNetBufferingTargetWrapper.cs +++ b/src/Shared/Targets/Wrappers/AspNetBufferingTargetWrapper.cs @@ -161,9 +161,16 @@ protected override void InitializeTarget() { var httpContextAccessor = HttpContextAccessor; // Best to resolve HttpContext before starting logging if (httpContextAccessor is null) + { InternalLogger.Debug("{0}: HttpContextAccessor not available", this); + } #if !ASP_NET_CORE + if (httpContextAccessor?.HttpContext != null) + { + OnBeginRequest(HttpContext.Current); + } + // Prevent double subscribe NLogHttpModule.BeginRequest -= OnBeginRequestHandler; NLogHttpModule.EndRequest -= OnEndRequestHandler; @@ -171,7 +178,7 @@ protected override void InitializeTarget() NLogHttpModule.BeginRequest += OnBeginRequestHandler; NLogHttpModule.EndRequest += OnEndRequestHandler; #endif - + base.InitializeTarget(); }