请问如何实现程序运行记录? 类似于trace,不过trace是调试时候用的因为程序崩溃后,进程也停止了,想保存位置到文件也来不及啊。不知道如何记录运行记录,大致知道问题在哪里 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{ int i2 = (int)o2; // Error} catch (Exception e){ string s1 = e.Message; //请把s1的内容写入你的日志 例如txt sqlserver ... //当然 Message是主要内容 如果想要更全的 你看看e的其它属性 我记得有个很全的} public static void WriteLog(string errorPath, Exception ex) { string path = System.Windows.Forms.Application.StartupPath + "\\" + errorPath; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } path = path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true); string methodname = trace.GetFrame(trace.FrameCount - 1).GetMethod().Name; string errorLine = trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber().ToString(); //错误方法名称+错误行+错误信息 string content = "MethodName:" + methodname + System.Environment.NewLine + "ErrorRowLine:" + errorLine + System.Environment.NewLine + "ErrorMessage:" + ex.Message; System.IO.File.WriteAllText(path, content, Encoding.Default); trace = null; } 请问kunkun0921:函数WriteLog怎么用呢,用在什么地方?可不可以给个例子参考下,谢谢。 C# 设置DPI Post中文编码问题 DataRow[] 绑定datagrid 问题? 帮忙解释一下几个概念. 请教:如何用程序清除本地的cookie C#通信速率如何才能达到最快??? 关于List对象集合转化成Json字符串,动态赋值问题 Web控件如何屏蔽网页弹窗 webform中数据库Update的问题 这是什么逻辑? winform 数值类型转化问题 SqlDataAdapter 相关问题
try
{
int i2 = (int)o2; // Error
} catch (Exception e)
{
string s1 = e.Message;
//请把s1的内容写入你的日志 例如txt sqlserver ...
//当然 Message是主要内容 如果想要更全的 你看看e的其它属性 我记得有个很全的
}
public static void WriteLog(string errorPath, Exception ex)
{
string path = System.Windows.Forms.Application.StartupPath + "\\" + errorPath;
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path = path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
string methodname = trace.GetFrame(trace.FrameCount - 1).GetMethod().Name;
string errorLine = trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber().ToString();
//错误方法名称+错误行+错误信息
string content = "MethodName:" + methodname + System.Environment.NewLine + "ErrorRowLine:" + errorLine + System.Environment.NewLine + "ErrorMessage:" + ex.Message; System.IO.File.WriteAllText(path, content, Encoding.Default);
trace = null;
}