5

I have an error handler in my global.asax as follows;

  Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        Dim ex = Server.GetLastError.GetBaseException
        Dim lastErrorWrapper As HttpException = Server.GetLastError()
        Dim lastError As Exception = lastErrorWrapper

        If lastErrorWrapper.InnerException IsNot Nothing Then
            lastError = lastErrorWrapper.InnerException
        End If

        My.ErrorHandler.LogError( _
            "<BR/><BR/>URL: " & Request.RawUrl & _
            "<BR/><BR/>STACK: " & ex.StackTrace & _
            "<BR/><BR/>SOURCE: " & ex.Source & _
            "<BR/><BR/>MESSAGE: " & ex.Message & _
            "<BR/><BR/>TYPENAME: " & ex.GetType.ToString & _
            "<BR/><BR/>INNER EXCEPTION: " & lastError.ToString & _
            "<BR/><BR/>REFERRER: " & HttpContext.Current.Request.Url.AbsoluteUri & _
            "<BR/><BR/>USER IP: " & Request.ServerVariables("REMOTE_ADDR") & " -- " & Request.ServerVariables("HTTP_USER_AGENT"))
    End Sub

Obviously, this works great and sends me an email whenever there is an error. But, this is also true for any images that are not found in the file system. It gives me a "File does not exist." error. Is there a way to ignore logging errors for images that are not located on disk?

Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104
  • 1
    Not really an answer, since it's a large deviation from your current solution, but with [ELMAH (Error Logging Modules and Handlers)](http://code.google.com/p/elmah/) you can configure this and many other aspects of error logging. – Michiel van Oosterhout Dec 23 '12 at 21:38
  • @michielvoo that is how I ended up with this problem. with NuGet it was quite easy to download ELMAH and set it up in just 10 secs. – Subliminal Hash Feb 20 '13 at 14:38

3 Answers3

5

Cant you resolve the FileNotFoundException instead? If the exception should not occur then rather resolve the issue than suppressing it. To handle a known exception you can use the following code.

if(Server.GetLastError().GetBaseException() is System.Web.HttpException)
{
     //You could check whether the 
     //Server.GetLastError().GetBaseException().Message contains the appropriate message
     Debug.WriteLine("Suppressed FileNotFoundException");
}else
//Log an unhandled exception
Jonathan
  • 2,318
  • 7
  • 25
  • 44
2
var ex = Context.Error;

if (ex is HttpException)
{
    var httpException = (HttpException)ex;

    if (httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
        return; // Do nothing 404    
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
halit
  • 1,128
  • 1
  • 11
  • 27
1

I know that this sounds very easy, or very simple, or you may search for the error code, but this is what I do - simple check if the error contains the message of dose not exist:

lastErrorWrapper.ToString().Contains("does not exist.")
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I have the same problem actually. Is there any way to get the name of the resource that is missing (does not exist)? Sometimes pages that do not exist need to be e-mailed to me so that an investigation can take place. – Clarice Bouwer Dec 21 '12 at 08:43
  • @kleinkie I log the full error and check them. I suggest you just to log it and not email it, because if some one for any reason start to try load many non existing pages, you will full with emails. In my server I have hundred of this line with "does not exist" each week. – Aristos Dec 21 '12 at 08:45
  • Thanks, that is what I do, I just sometimes forget to check the errors. I will just write a simple daily report which will automatically be e-mailed to me so that I can check for any suspicious errors. =) – Clarice Bouwer Dec 21 '12 at 08:52