How to ignore logging errors for images in global asax
Asked Answered
G

3

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?

Guadalajara answered 18/12, 2012 at 7:40 Comment(2)
Not really an answer, since it's a large deviation from your current solution, but with ELMAH (Error Logging Modules and Handlers) you can configure this and many other aspects of error logging.Handful
@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.Guadalajara
H
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
Hygrometric answered 21/12, 2012 at 10:10 Comment(0)
A
2
var ex = Context.Error;

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

    if (httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
        return; // Do nothing 404    
}
Adoration answered 25/12, 2012 at 17:1 Comment(0)
S
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.")
Score answered 18/12, 2012 at 8:26 Comment(3)
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.Unicycle
@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.Score
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. =)Unicycle

© 2022 - 2024 — McMap. All rights reserved.