Debug技巧-------控制台程序或者应用程序输出错误到txt
2013-07-12 17:40
239 查看
控制台程序在服务器上运行错误时容易崩溃,无法找到原因
为了找到原因 我们可以 把 错误输出 在txt中
代码如下:
可能出错的地方 可以
应用程序中 也可以 用
或者
为了找到原因 我们可以 把 错误输出 在txt中
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace Test { class Program { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var ss = string.Format("{0}发生系统异常。\r\n{1}\r\n\r\n\r\n", DateTime.Now, e.ExceptionObject.ToString()); File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "系统异常.log"), ss); // MessageBox.Show("应用程序未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); // Application.Exit(); } }
可能出错的地方 可以
try {} catch{ throw new Exception("error: "); }
应用程序中 也可以 用
File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "系统异常.log"), ss);
或者
try { executeInsert(sSQLHeader + sSqlEntity.Substring(0, sSqlEntity.Length - 1)); closeConnection(); } catch { System.IO.StreamWriter sw1 = System.IO.File.AppendText("log.txt"); sw1.WriteLine(DateTime.Now.ToString()); sw1.WriteLine(sSQLHeader + sSqlEntity.Substring(0, sSqlEntity.Length - 1)); sw1.Flush(); sw1.Close(); }
相关文章推荐
- Debug技巧-------控制台程序或者应用程序输出错误到txt
- 错误 83 未能写入输出文件“F:\\obj\Debug\CourseArrangeManage.exe”--另一个程序正在使用此文件,进程无法访问。
- 习题8:编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。
- Web应用程序或者WinForm程序 调用 控制台应用程序及参数传递
- WIN32程序挂钩SetLastError,输出错误描述到控制台
- 控制台应用程序转成MFC程序错误—OcrRec.exe触发一个触点,原因可能是堆被破坏
- WIN32程序挂钩SetLastError,输出错误描述到控制台
- Tips: 如何让自己的应用程序,控制台程序,windows 服务收到操作系统注销或者重启的消息?
- Linux 程序开发打印 Debug 信息的使用技巧--C语言中几种输出调试信息的方法
- 调用控制台应用程序并获得程序的输出信息
- 调用控制台应用程序并获得程序的输出信息
- 如何让自己的应用程序,控制台程序,windows 服务收到操作系统注销或者重启的消息
- 【调试中非常重要的技巧】如何从错误堆栈的输出判断程序出错的准确位置?
- WIN32程序挂钩SetLastError,输出错误描述到控制台
- 解决Installer重新安装补丁错误-"无法打开此修补程序包。请确认该修补程序包存在,并且您可以访问它,或者与应用程序供应商联系,以确认这是否是一个有效的 Windows Installer 修补程序包。"
- 控制台应用程序中调用CFileException 的GetErrorMessage方法时出现“Debug Assertion Failed”错误时的解决办法
- Linux 程序开发打印 Debug 信息的使用技巧--C语言中几种输出调试信息的方法
- MFC程序中Debug模式下调试信息输出到控制台
- WIN32程序挂钩SetLastError,输出错误描述到控制台
- 获取控制台程序(Console)的输出内容