Application_BeginRequest not being fired in stressed environment
<p>We are obesrving strange problems when performing sress test of our applciation. We use Application_BeginRequest and Application_EndRequest to log the beginning and end of the web request, together, with the thread id.</p>

<p>However, from our logs, we see that Application_Begin_REquest is not being fired:</p>

<p>We use the following code to do the logging in global.asax.cs:</p>

<pre><code>protected void Application_BeginRequest(object sender, EventArgs e)
{
  string url = "";
  if (HttpContext.Current != null)  // this should alway be true
    url = HttpContext.Current.Request.Url.ToString();

  Dbg.WriteLine(String.Format("Request: {0} {1}", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"], url));

  // integration calls measurement
  HttpContext.Current.Items.Add("wcfElapsed", new TimeSpan());

}

protected void Application_EndRequest(Object sender, EventArgs e)
{
  string url = "";
  if (HttpContext.Current != null)  // this should alway be true
    url = HttpContext.Current.Request.Url.ToString();

  Dbg.WriteLine(String.Format("End request: {0} {1}", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"], url));
}
</code></pre>

<p>This is our log file. Urls are omitted The 00013 column is thread id.</p>

<pre><code>  14.12.10 21:41:25.042 00013 00000            Request: 172.23.26.41 
  14.12.10 21:41:25.068 00013 00000            End request: 172.23.26.41 
  14.12.10 21:41:25.212 00013 00000            Request: 172.23.26.41 
  14.12.10 21:41:25.223 00013 00000            End request: 172.23.26.41 
  14.12.10 21:41:30.974 00013 00000            End request: 172.23.26.88 
</code></pre>

<p>You can see that there are two "End request" in the last two lines, but there is no (Begin) Request for the last log line.</p>

<p>Our Dbg.WriteLine uses System.Diagnostics trace listeners to ouput data to the file.</p>

<p>Environment: Windows Server 2008 R2, ASP.NET 3.5</p>

<p>This only happens when performin stress test. CPU utilization is around 60%, there are max 10 concurent requests executing. </p>

<p>Any ideas, what could be wrong?</p>

<p><strong>UPDATE:</strong> I have found out that some other also did have similar problems (althoug in different configuration: <a href="http://forums.iis.net/t/1154954.aspx" rel="nofollow">http://forums.iis.net/t/1154954.aspx</a>)
Matej</p>

<p><strong>UPDATE#2</strong>: This night be related to fact, that Thread.GetHashCode() which is used to print out the thread identifier in our log fies could change. See <a href="http://stackoverflow.com/questions/5112333/asp-net-thread-gethashcode-changes">ASP.NET - Thread.GetHashCode() changes</a></p>
