类似于trace,不过trace是调试时候用的因为程序崩溃后,进程也停止了,想保存位置到文件也来不及啊。不知道如何记录运行记录,大致知道问题在哪里

解决方案 »

  1.   


    try
    {
        int i2 = (int)o2;   // Error
    } catch (Exception e)
    {
        string s1 = e.Message;
       //请把s1的内容写入你的日志 例如txt sqlserver ...
       //当然 Message是主要内容 如果想要更全的 你看看e的其它属性 我记得有个很全的
    }
      

  2.   


     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;
            }
      

  3.   

    请问kunkun0921:函数WriteLog怎么用呢,用在什么地方?可不可以给个例子参考下,谢谢。