C# 后台报错输出到日志
2024-09-02 12:02:40
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);
}
}
}
}
后续完善
最新文章
- 微信开发中遇到“当前页面的url未注册”问题
- JAVASCRIPT中经典面试题
- 使用了Windows Live Writer 写的博客
- 调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍
- 【多线程】Java并发编程:Lock(转载)
- 关于iOS中SQLITE句柄的使用的细节
- Joseph cicyle&#39;s algorithm
- Nyoj 43 24 Point game 【DFS】
- yii操作数据库(AR)
- Struts2 一张图片引发的bug
- 实现strcmp非常easy的思维
- codeforces 466d Increase Sequence
- 阿里ECS配置MSSQL远程连接的坑
- Linux批量结束、杀死进程
- 详细集成Redis (一)
- C# log4net 使用
- BZOJ4541 HNOI2016矿区(平面图转对偶图)
- C# 各种控件实现可拖动和调整大小
- shell编程小结
- cmd.exe启动参数详解