C#中的异常处理
while (ex != null)
{
WriteExceptionLog(ex, fileName);
ex = ex.InnerException;
}
/// <summary>
/// 将异常处理成字符串的形式,然后写入文件
/// </summary>
/// <param name="ex"></param>
/// <param name="fileName"></param>
private void WriteExceptionLog(Exception ex, string fileName)
{
WriteLog(string.Format("错误信息:{0}{1}异常类型:{2}{1}程序集:{3}{1}方法:{4}{1}跟踪栈信息:{1}{5}{1}",
ex.Message,
Environment.NewLine,
ex.GetType(),
ex.Source,
ex.TargetSite,
ex.StackTrace),
fileName);//写入文件
}