2

I am working on some error handling code and I want to determine if its a local connection (developer) or not. Typically I look at HttpContext.Current.Request.ServerVariables however in the error handling code if authentication expired HttpContext.Current is null.

When HttpContext.Current is null, how do I go about getting the server variables--or at least the IP address of the client that kicked off the request--so I can determine if its a developer on their own local server or not.

UPDATE: I was able to retrieve the relevant server variables in the ErrorMailEventArgs parameter passed in by elmah, specifically errorMailEventArgs.Error.ServerVariables.

Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
  • 2
    When (in terms of page lifecycle) and where are you trying to do the error handling? I'm just wondering what would cause HttpContext.Current to be null in the first place. – rsbarro Apr 07 '11 at 20:16
  • The request triggers an error caught by ELMAH which finishes the response but then in another thread ELMAH continues to process the error and send out the e-mail. – Aaron Silverman Apr 07 '11 at 21:35
  • OK, so in what part of that process are you trying to access the ServerVariables, when ELMAH catches the error or when it goes to process the error and send out the email? – rsbarro Apr 07 '11 at 22:10
  • @rsbarro trying to access in the ErrorMail_Mailing module call--which is preprocessing done right before sending out the e-mail – Aaron Silverman Apr 08 '11 at 20:02

3 Answers3

1

I was able to retrieve the relevant server variables in the ErrorMailEventArgs parameter passed in by elmah, specifically errorMailEventArgs.Error.ServerVariables.

Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
  • 1
    Nice! I think this issue occurs because HttpContext.Current is not available on background threads (please note I haven't verified this myself). See the accepted answer to this question, it indicates the same thing: http://stackoverflow.com/questions/2638345/safe-httpcontext-current-cache-usage – rsbarro Apr 07 '11 at 22:40
  • Great link rsbarro, this is exactly what is going on – Aaron Silverman Apr 08 '11 at 20:03
  • Cool, do you want me to post that comment as an answer? – rsbarro Apr 08 '11 at 20:07
  • Its cool I will just accept this one as it contains both what you do and the reason why, which should hopefully help out others with same question. Thanks! – Aaron Silverman Apr 10 '11 at 16:07
  • OK, sounds good. Was just asking because I saw you hadn't accepted your answer earlier. Glad you were able to figure out what was going on. – rsbarro Apr 10 '11 at 16:57
0

The Request object should get you the requesting IP.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
0

The request object has these properties:

Request.UserHostAddress

MSDN Link

  • System.Web.HttpRequest doesn't have any static methods and I don't have a page object in Elmah Error land :( – Aaron Silverman Apr 07 '11 at 22:13
  • It not a static method. Here is the MSDN page for reference. http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx –  Apr 07 '11 at 22:30