Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异常日志记录HttpModule不';无法捕获ASP.NET页面方法中的错误_Asp.net_Webforms - Fatal编程技术网

异常日志记录HttpModule不';无法捕获ASP.NET页面方法中的错误

异常日志记录HttpModule不';无法捕获ASP.NET页面方法中的错误,asp.net,webforms,Asp.net,Webforms,我们有一个HttpModule,用于捕获异常并将其记录到数据库中。它看起来像这样: public class ExceptionLoggingModule : IHttpModule { public void Init(HttpApplication context) { context.Error += OnError; } private static void OnError(object sender, EventArgs e)

我们有一个HttpModule,用于捕获异常并将其记录到数据库中。它看起来像这样:

public class ExceptionLoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Error += OnError;
    }

    private static void OnError(object sender, EventArgs e)
    {
        try
        {
            var context = (HttpApplication) sender;
            var exception = context.Server.GetLastError();

            if (exception != null)
            {
                // Log exception
            }
        }
        catch(Exception)
        {
        }
    }
}
这在一般情况下是有效的,但我刚刚注意到,当页面方法(即标记为WebMethod属性的代码隐藏文件中的方法)中发生错误时,OneError方法从不触发

为什么


除了在Page方法内部重新实现异常日志记录之外,我还能做些什么吗?

我在这里找到了一个适合我的解决方案:

以下是我编写的处理程序的要点:

public class PathfinderModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PostMapRequestHandler += this.OnPostMapRequestHandler;
        context.Error += OnError;
    }

    private void OnPostMapRequestHandler(object sender, EventArgs e)
    {
        Page aux = HttpContext.Current.Handler as Page;

        if (aux != null)
        {
            aux.Error += this.OnPageError;
        }
    }

    private static void OnError(object sender, EventArgs e)
    {
        // Blah..
    }

    private void OnPageError(object sender, EventArgs e)
    {
        // Blah...
    }        
}

我今天也遇到了同样的问题。嗯,这对我来说不起作用-如果我从PageMethod内部引发异常,事件处理程序不会启动。第1部分:检查基本情况-web.config包含正确的设置,并且这些设置的格式/拼写正确(我已经被发现过几次)。重新启动IIS/应用程序池以确保安全。在
Init()
方法中放置一个断点,并将Visual Studio连接到承载站点的w3wp.exe进程。您想知道代码是否正在执行第2部分:如果attch方法不起作用,请在代码中放入一些
Trace.WriteLine()
调用,以便查看是否正在调用该代码。使用诸如来自Sysinternals的DebugOutput之类的工具来获取WriteLine调用。如果您的代码根本没有执行,那么我不确定它可能是什么,抱歉。您好,谢谢,通过所有必要的断点,我可以看到它在正常页面生命周期中引发异常时执行,但在PageMethods引发异常时不执行。好的,可能值得您打开一个新问题,并参考此问题,并张贴您正在使用的代码。听起来你可能有一个不同的问题,然后我所遭受的。如果你用自己的问题来讨论,会更容易帮助你。