Elmah异常处理 [英] Exception handling with Elmah

查看:178
本文介绍了Elmah异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我抓住并重新抛出发生在各种类中的异常,方法,并将其记录到程序的主要try-catch块中的Elmah。



//主程序

  try 
{
//某些代码触发其他类,等...
MyTestClass myTestClass = new MyTestClass();
myTestClass.Execute();

}
catch(Exception ex)
{
ErrorSignal.FromCurrentContext()。
}

// MyTestClass

  public class MyTestClass 
{
public object ApiResult {get;组; }


public string Execute()
{
try
{
//执行一些代码
// .. ..
//设置xml消息
ApiResult =用户信息xml响应;
}
catch(Exception ex)
{
//设置xml消息
ApiResult =发生错误的XML错误响应...;

throw;
}
}

}

最好记录发生的例外情况?另一个问题,我应该记录我可以处理的错误,而不捕捉异常?例如,如果某些东西是空的,我应该做一个测试(如果是null ...)并在Elmah中记录一条消息?

解决方案

而不是使用Elmah的 ErrorSignal 类手动记录错误,您应该自行尝试让ELMAH自动记录错误,这是在应用程序的

在您的示例中,主程序存在严重问题。也就是说,它是吞下异常,至少对于最终用户。是的,异常是在ELMAH中登录,但您正在隐藏用户的错误。最终用户会认为她的表单提交(或任何)通过没有错误,实际上有一个严重的问题。



总之, try ... catch 块应该谨慎使用,例如在可以从错误中恢复的情况下,或者错误是次要的错误时,不应该停止工作流。但大多数错误是真正的显示屏,没有优雅的解决方法。对于这个大多数人来说,你想让错误渗透到ASPMA运行时,ELMAH会自动记录它,用户将在哪里看到一个错误页面,提醒他们发生错误的事实。



查看我的这篇文章: ASP.NET Web异常处理建议应用程序


I log exceptions with Elmah and was wondering if the technique I am using is good design?

Right now I catch and re throw exceptions that occur in various classes and methods, and log them to Elmah in the main try-catch block of the program.

// Main Program

try
{
   // Some code that fires off other classes, etc...
   MyTestClass myTestClass = new MyTestClass();
   myTestClass.Execute();   

}
catch(Exception ex)
{
   ErrorSignal.FromCurrentContext().Raise(ex);
}

// MyTestClass

    public class MyTestClass
    {
        public object ApiResult { get; set; }


        public string Execute()
        {            
            try
            {
                // execute some code
                // ....
                // set xml message
                ApiResult = "User information xml response";
            }
            catch (Exception ex)
            {
                // set xml message
                ApiResult = "something went wrong xml error response...";

                throw;
            }
        }

    }

Would it be better to log the exceptions where they occur? Another question, should I log errors that I can handle without catching exceptions? For example if something is null, should I do a test for that (if null...) and log a message in Elmah?

解决方案

Rather than manually logging an error using Elmah's ErrorSignal class you should instead strive to let ELMAH log errors for you automatically, which occurs when the application's Error event is raised.

In your example there's a serious problem with Main Program. Namely, it is swallowing exceptions, at least for the end user. Yes, the exception is getting logged in ELMAH but you are hiding the error from the user. The end user will think her form submission (or whatever) went through without error, when in actuality there was a grave problem.

In short, try...catch blocks should only be used sparingly, such as in cases where you can recover from an error or when the error is a "minor" one and should not stop the workflow. But the majority of errors are real show stoppers and don't have graceful workaround. For this majority you'd want to let the error percolate up to the ASP.NET runtime where ELMAH will automatically log it and where the user will see an error page, alerted them to the fact that an error has occurred.

Check out this article of mine: Exception Handling Advice for ASP.NET Web Applications.

这篇关于Elmah异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆