Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ben's answer is right on.</p> <p>However, like some of the other users, I was still a bit lost on how to proceed. This <a href="http://piers7.blogspot.com/2005/12/log4net-context-problems-with-aspnet.html" rel="nofollow noreferrer">log4net Context problems with ASP.Net thread agility</a> post and especially this <a href="https://web.archive.org/web/20160429190047/http://blog.marekstoj.com/2011/12/log4net-contextual-properties-and.html" rel="nofollow noreferrer">Marek Stój's Blog - log4net Contextual Properties and ASP.NET</a> one give some more context for the problem with some excellent code examples.</p> <p>I highly recommend Marek Stój's implementation, although the <code>ThreadContext.Properties["UserName"]</code> needed to be replaced with <code>ThreadContext.Properties["User"]</code> in my case.</p> <p>I added a BeginRequest method to my Logger class which I call from Application_AuthenticateRequest which loads all the relevant log4net properties.</p> <pre><code>protected void Application_AuthenticateRequest(object sender, EventArgs e) { Logger.BeginRequest(Request); } </code></pre> <p>And the method code:</p> <pre><code>public static void BeginRequest(System.Web.HttpRequest request) { if (request == null) return; ThreadContext.Properties["ip_address"] = AdaptivePropertyProvider.Create("ip_address", IPNetworking.GetMachineNameAndIP4Address()); ThreadContext.Properties["rawUrl"] = AdaptivePropertyProvider.Create("rawUrl", request.RawUrl); if (request.Browser != null &amp;&amp; request.Browser.Capabilities != null) ThreadContext.Properties["browser"] = AdaptivePropertyProvider.Create("browser", request.Browser.Capabilities[""].ToString()); if (request.IsAuthenticated &amp;&amp; HttpContext.Current.User != null) ThreadContext.Properties["User"] = AdaptivePropertyProvider.Create("user", HttpContext.Current.User.Identity.Name); } </code></pre> <p>I found I had to pass in the Request object instead of using <code>HttpContext.Current.Request</code> within the method. Otherwise I would loose the user and authentication information. Note that the <code>IPNetworking</code> class is my own so you will need to provide your own method of obtaining the client IP. The <code>AdaptivePropertyProvider</code> class is directly from Marek Stój.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload