Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify when Application_BeginRequest and Application_EndRequest tie up

Tags:

rest

.net

I want to add some logging of how long it takes for a .Net Web Application is taking to process a request.

My idea was to use Application_BeginRequest and Application_EndRequest, I could check HttpContext.Current.Request.Path in Application_BeginRequestand log a start-time and when the same HttpContext.Current.Request.Path goes through Application_EndRequest log the end-time. But with a busy website I could not guarantee that multiple calls would mess this idea up (a call may come in that takes longer to run than an identical call).

So my question is, is there something that I can check in Application_BeginRequest and Application_EndRequest that ties the calls together, is there some ID or something? Or is there something more simple that I could be doing?

like image 671
sbarnby71 Avatar asked Apr 09 '26 16:04

sbarnby71


1 Answers

My solution for this was to use HttpContext.Current.Items as suggested by ta.speot.is

In Application_BeginRequest I create a System.Diagnostics.Stopwatch and call .Start on it.

Add the Stopwatch to HttpContext.Current.Items.

In Application_EndRequest retrieve the Stopwatch from HttpContext.Current.Items and call .Stop I can now check .Elapsed and that gives me my timings.

When I posted my question I was not aware that HttpContext.Current.Items is specific to a call, but I do now - so this provides the solution.

like image 59
sbarnby71 Avatar answered Apr 12 '26 08:04

sbarnby71