C# 后台报错输出到日志
1.C# 方法
/// <summary> /// 异常处理 /// </summary> /// <returns></returns> public void GetLogInfo() { string msg = string.Empty; try { string str="这是程序代码"; } catch (Exception ex) { msg = ex.Message; isSavedSuccessfully = false; WoExcepitonLog wl = new WoExcepitonLog(); wl.WriteLog(ex, "WO_Exception.txt"); wl.CreateLog(ex, (this.SessionExt().Get<SessionUser>() as SessionUser).UserId, WOExceptionType.SaveException, ""); throw new TeldWOException(WOExceptionType.SaveException, ex); } }
2.输出日志方法
public class ExcepitonLog
{
private static object m_Lock = new object();
string dic = @"C:\Logs";
string RealTimeData = "WO_RealTimeData.txt";//运行数据
string RealTimeException = "WO_Exception.txt";//异常
public void WriteLog(Exception ce, string fileName)
{
if (!Directory.Exists(dic))//判断是否存在
{
Directory.CreateDirectory(dic);//创建新路径
}
lock (m_Lock)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(dic + "\\" + fileName, true))
{
file.WriteLine("------------------------------" + DateTime.Now.ToString() + "------------------------------");
file.WriteLine(ce.StackTrace);
file.WriteLine(ce.Source);
file.WriteLine(ce.TargetSite);
file.WriteLine(ce.Message);
}
}
}
}
后续完善
浙公网安备 33010602011771号