我記錄了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;
}
}
}
它會更好登錄他們出現異常?另一個問題,我應該記錄我可以處理的錯誤而不捕捉異常嗎?例如,如果某些內容爲空,我應該對該內容進行測試(如果爲null ...)並在Elmah中記錄消息?
錯誤日誌記錄是針對需要吐出xml錯誤消息的web服務的,這就是爲什麼我不會在主循環中拋出異常的原因。 – chobo
@chobo:我重申了你的問題,以更準確地反映你的問題。 –
在這種情況下,在web方法中,我將錯誤響應字符串設置爲Exception.Message,使用Elmah記錄異常(我個人在Exception上使用擴展方法來執行此操作),併吞下異常。大體上你做什麼除了重新投擲。 –