使用 Elmah 进行异常处理
我用 Elmah 记录异常,想知道我使用的技术是否是好的设计?
现在,我捕获并重新抛出各种类和方法中发生的异常,并将它们记录到程序的主 try-catch 块中的 Elmah。
// 主程序
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;
}
}
}
将异常记录在发生的地方会更好吗?另一个问题,我应该记录可以在不捕获异常的情况下处理的错误吗?例如,如果某些内容为空,我应该对此进行测试(如果为空...)并在 Elmah 中记录一条消息吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该努力让 ELMAH 自动为您记录错误,而不是使用 Elmah 的
ErrorSignal
类手动记录错误,这会在引发应用程序的Error
事件时发生。在您的示例中,主程序存在严重问题。也就是说,它吞噬了异常,至少对于最终用户而言是如此。是的,例外情况是登录 ELMAH,但您向用户隐藏了错误。最终用户会认为她的表单提交(或其他任何内容)没有错误,但实际上存在严重问题。
简而言之,应该谨慎使用
try...catch
块,例如在可以从错误中恢复的情况下,或者错误是“次要”错误并且不应停止工作流程时。但大多数错误都是真正的阻碍,并且没有优雅的解决方法。对于大多数人来说,您希望让错误渗透到 ASP.NET 运行时,ELMAH 将在其中自动记录错误,并且用户将在其中看到错误页面,提醒他们发生了错误。查看我的这篇文章:ASP 的异常处理建议.NET Web 应用程序。
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'sError
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.
我认为以下情况会让您了解何时使用 ErrorSignal.FromCurrentContext().Raise(ex);
Email.SendProfileChangedNotification 方法在这段代码中并不是那么重要,我的意思是,如果它有任何错误,我可以保留它,并且我不想将它们显示给用户。重要的是,他/她的个人资料已更新,并且用户可以通过查看个人资料页面来了解这一点。
所以我相信代码中的某些地方可能会不断失败,我想收到有关它们的通知,但我不想分解整个操作。
I think the following situation will give you an idea for when to use ErrorSignal.FromCurrentContext().Raise(ex);
Email.SendProfileChangedNotification method is not so important in this code, I mean, I can leave with it if it has any errors and I don't want to show them to the user. The important part is that his/her profile is updated and the user is aware of it by seeing the profile page.
So I believe there places in code which may fail constantly, I would like to get notified about them, but I don't want to break down the entire action.