【发布时间】:2011-12-05 12:10:05
【问题描述】:
我用 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 中记录一条消息?
【问题讨论】:
标签: c# web-services exception error-handling elmah