ELMAH 过滤编程不起作用

发布于 2024-07-29 23:48:41 字数 3212 浏览 1 评论 0原文

Elmah 总是介入。如何以编程方式过滤: 这是我的全局文件:

    /// <summary>
    /// Handles the Filtering event of the ErrorLog control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        Filter(e);
    }

    /// <summary>
    /// Handles the Filtering event of the ErrorMail control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        Filter(e);
    }

    /// <summary>
    /// Filters the specified e.
    /// </summary>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    private void Filter(ExceptionFilterEventArgs e)
    {
        Exception exception = e.Exception.GetBaseException();
        var httpException = exception as HttpException;

        if (httpException != null && httpException.GetHttpCode() == 404)
        {
            e.Dismiss();
        }

        if (exception is FileNotFoundException ||
            exception is HttpRequestValidationException ||
            exception is ViewStateException ||
            exception is CryptographicException ||
            exception is NodeNullException ||
            exception is SectionNullException ||
            exception is IdentifierNullException ||
            exception is AccessForbiddenException ||
            exception is HttpException)
        {
            e.Dismiss();
        }
    }

    /// <summary>
    /// Handles the Error event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Application_Error(Object sender, EventArgs e)
    {
        bool errorObtained = false;
        Exception ex = null;

        try
        {
            Exception rawException = Server.GetLastError();
            if (rawException != null)
            {
                errorObtained = true;
                if (rawException.InnerException != null)
                {
                    ex = rawException.InnerException;
                }
                else
                {
                    ex = rawException;
                }
            }
        }
        catch
        {
        }

        if (errorObtained && ex != null)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
            {
                if (HttpContext.Current.IsCustomErrorEnabled)
                {
                    Server.Transfer(errorPage, false);
                }
            }
        }
    }

Elmah always kick in. How to filter programmatically:
Here is my Global file:

    /// <summary>
    /// Handles the Filtering event of the ErrorLog control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        Filter(e);
    }

    /// <summary>
    /// Handles the Filtering event of the ErrorMail control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        Filter(e);
    }

    /// <summary>
    /// Filters the specified e.
    /// </summary>
    /// <param name="e">The <see cref="Elmah.ExceptionFilterEventArgs"/> instance containing the event data.</param>
    private void Filter(ExceptionFilterEventArgs e)
    {
        Exception exception = e.Exception.GetBaseException();
        var httpException = exception as HttpException;

        if (httpException != null && httpException.GetHttpCode() == 404)
        {
            e.Dismiss();
        }

        if (exception is FileNotFoundException ||
            exception is HttpRequestValidationException ||
            exception is ViewStateException ||
            exception is CryptographicException ||
            exception is NodeNullException ||
            exception is SectionNullException ||
            exception is IdentifierNullException ||
            exception is AccessForbiddenException ||
            exception is HttpException)
        {
            e.Dismiss();
        }
    }

    /// <summary>
    /// Handles the Error event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Application_Error(Object sender, EventArgs e)
    {
        bool errorObtained = false;
        Exception ex = null;

        try
        {
            Exception rawException = Server.GetLastError();
            if (rawException != null)
            {
                errorObtained = true;
                if (rawException.InnerException != null)
                {
                    ex = rawException.InnerException;
                }
                else
                {
                    ex = rawException;
                }
            }
        }
        catch
        {
        }

        if (errorObtained && ex != null)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
            {
                if (HttpContext.Current.IsCustomErrorEnabled)
                {
                    Server.Transfer(errorPage, false);
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-08-05 23:48:41

我认为您必须将 HttpModule 命名为:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

使用上面指定的确切名称

I think you have to name your HttpModules as:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

with the exact names specified above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文