Jad*_*ias 5 .net c# logging refactoring
在我的服务中,所有暴露方法都有:
try
{
// the method core is written here
}
catch(Exception ex)
{
Log.Append(ex);
}
Run Code Online (Sandbox Code Playgroud)
一遍又一遍地重复它是无聊和丑陋的.有什么办法可以避免吗?有没有更好的方法来保持服务正常工作,即使发生异常并继续向Log类发送异常详细信息?
您可以为所有未捕获的异常设置通用错误处理方法,如下所示:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
Run Code Online (Sandbox Code Playgroud)
根据出现的问题,您可能无法从错误中恢复......但这应该可以让您了解出了什么问题。如果应用程序代码无法正常处理异常,则此方法可能会尝试将服务重新初始化为已知的工作状态。