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.

4
  • 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. Commented Apr 7, 2011 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. Commented Apr 7, 2011 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? Commented Apr 7, 2011 at 22:10
  • @rsbarro trying to access in the ErrorMail_Mailing module call--which is preprocessing done right before sending out the e-mail Commented Apr 8, 2011 at 20:02

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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: stackoverflow.com/questions/2638345/…
Great link rsbarro, this is exactly what is going on
Cool, do you want me to post that comment as an answer?
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!
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.
0

The Request object should get you the requesting IP.

Comments

0

The request object has these properties:

Request.UserHostAddress

MSDN Link

2 Comments

System.Web.HttpRequest doesn't have any static methods and I don't have a page object in Elmah Error land :(
It not a static method. Here is the MSDN page for reference. msdn.microsoft.com/en-us/library/system.web.httprequest.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.