5

次のように、global.asax にエラー ハンドラーがあります。

  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

明らかに、これはうまく機能し、エラーが発生するたびにメールが送信されます。ただし、これは、ファイル システムで見つからないイメージにも当てはまります。「ファイルが存在しません」と表示されます。エラー。ディスク上にないイメージのログ エラーを無視する方法はありますか?

4

3 に答える 3

5

代わりに FileNotFoundException を解決できませんか? 例外が発生しない場合は、問題を抑制するのではなく、問題を解決してください。既知の例外を処理するには、次のコードを使用できます。

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
于 2012-12-21T10:10:27.490 に答える
2
var ex = Context.Error;

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

    if (httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
        return; // Do nothing 404    
}
于 2012-12-25T17:01:12.763 に答える
1

これは非常に簡単、または非常に単純に聞こえるかもしれませんが、エラー コードを検索することもできますが、これが私が行うことです。エラーに次のメッセージが含まれているかどうかを簡単に確認しますdose not exist

lastErrorWrapper.ToString().Contains("does not exist.")
于 2012-12-18T08:26:01.150 に答える