程序异常自动捕获记录日志之C#设计笔记(十二)

12 篇文章 0 订阅

一、自定义异常类
///
/// 自定义异常
///
[Serializable()]
public class SelException : System.Exception
{
private SelErrorType _selErrorType;
public SelErrorType selErrorType
{
get
{
return _selErrorType;
}
}
private SelLogLevel _selLogLevel;
public SelLogLevel selLogLevel
{
get
{
return _selLogLevel;
}
}
public SelException() : base() { }
public SelException(string message) : base(message) { }
public SelException(string message, System.Exception inner) : base(message, inner) { }
protected SelException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) { }
public SelException(SelErrorType ErrorType, SelLogLevel logLevel, string message)
: base(message)
{
_elErrorType = ErrorType;
_elLogLevel = logLevel;
}
}
二、异常类型
public enum SelErrorType
{
Error_Unknow,
Error_Data_UnKnow,
Error_Date_Type,
Error_Data_Uploading,
}
三、异常级别
public enum SelLogLevel
{
Log_Information,
Log_Debug,
Log_Error,
}
四、定义异常统一处理接口类
class FrmCommons
{
///
/// 异常统一处理
///
///
///
public static void SelExceptionHandle(object sender, ThreadExceptionEventArgs e)
{
try
{
if (typeof(SelException) == e.Exception.GetType())
{
SelException selEx = e.Exception as SelException;
if (selEx.selLogLevel >= SelLogLevel.Log_Information)
{
WriteSelExLog(selEx);
}
MsgBox.Error(Resources.ResourceManager.GetString(selEx.selErrorType.ToString(), Resources.Culture));
}
else
{
WriteSelExLog(e.Exception);
MsgBox.Error(e.Exception.Message);
}
}
catch (Exception ex)
{
}
}
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (typeof(SelException) == e.GetType())
{
SelException selEx = e.ExceptionObject as SelException;
if (selEx.selLogLevel >= SelLogLevel.Log_Information)
{
WriteSelExLog(selEx);
}
MsgBox.Error(Resources.ResourceManager.GetString(selEx.selErrorType.ToString(), Resources.Culture));
}
else if (typeof(InvalidOperationException) == e.ExceptionObject.GetType())
{
WriteSelExLog(e.ExceptionObject as InvalidOperationException);
}
else
{
WriteSelExLog(e.ExceptionObject as Exception);
}
}
catch (Exception ex)
{
}
}
private static void WriteSelExLog(Exception ex)
{
//将异常写入日志
string errorMsg = ex.Message + Environment.NewLine + “\t Stack Trace:” + Environment.NewLine + “\t”;
string strTmp = ex.StackTrace.Replace(“\n”,“\n\t”);
errorMsg += strTmp;
Thread t = new Thread(new ParameterizedThreadStart(LogHelper.WriteLog));
t.Start(errorMsg);
}
}
五、入口main函数中注册异常触发事件
1、在发生未捕获线程异常时
Application.ThreadException += new ThreadExceptionEventHandler(FrmCommons.SelExceptionHandle);
2、在某个异常未被捕获时出现
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(FrmCommons.CurrentDomain_UnhandledException);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值