在网上搜,捕获的异常放到日志中。源码还挺多,但是存在问题。一头一尾少一个字母。
修改了一下可以运行了。
public static void WriteErorrLog(Exception ex)
{
if(ex == null)
return; //ex = null 返回
StreamWriter write = null;
DateTime dt = DateTime.Now; // 设置日志时间
string time = dt.ToString("yyyy-mm-dd HH:mm:ss"); //年-月-日 时:分:秒
string LogName = "Log.log"; //日志名称
string LogPath = "C://Error//"; //日志存放路径
string Log = LogPath + LogName; //路径 + 名称
if(!File.Exists(Log)) //是否存在
{
Directory.CreateDirectory(LogPath); //创建文件夹
File.CreateText(LogName); // 创建日志
}
else
{
//追加,添加错误信息;
}
write = File.AppendText(Log);
write.WriteLine("异常时间:"+time); //
write.WriteLine("异常对象"+ex.Source);
write.WriteLine("调用堆栈"+ex.StackTrace.Trim());
write.WriteLine("异常信息:"+ex.ToString());
write.WriteLine("/r/n-----------------");
write.Flush();
write.Dispose();
//write.Dispse();
} 在catch中使用。
catch (Exception em)
{
WriteErorrLog(em);
return;
}
336





