我正在使用以下方法,该方法有效并大大减少了代码量(但我不确定是否有更好的方法或它的陷阱可能是什么。每当你打电话时:我怀疑给出减号的人会很有礼貌足以澄清他们的行为;)
try
{
CallTheCodeThatMightThrowException()
}
catch (Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
Utils.ErrorHandler.Trap ( ref objUser, st, ex );
} //eof catch
这里是 ErrorHandler 代码:为了清楚起见 - objUser - 是建模 appusers 的对象(您可能会获得诸如域名、部门、区域等信息以用于日志记录目的 ILog logger - 是日志记录对象 - 例如执行日志记录活动 StackTrace st - StackTrace 对象为您提供应用程序的调试信息
using System;
using log4net; //or another logging platform
namespace GenApp.Utils
{
public class ErrorHandler
{
public static void Trap ( Bo.User objUser, ILog logger, System.Diagnostics.StackTrace st, Exception ex )
{
if (ex is NullReferenceException)
{
//do stuff for this ex type
} //eof if
if (ex is System.InvalidOperationException)
{
//do stuff for this ex type
} //eof if
if (ex is System.IndexOutOfRangeException)
{
//do stuff for this ex type
} //eof if
if (ex is System.Data.SqlClient.SqlException)
{
//do stuff for this ex type
} //eof if
if (ex is System.FormatException)
{
//do stuff for this ex type
} //eof if
if (ex is Exception)
{
//do stuff for this ex type
} //eof catch
} //eof method
}//eof class
} //eof namesp