static void Main(String[] args)
{
	if (!Directory.Exists(Application.StartupPath + "\\Log"))
	{
		Directory.CreateDirectory(Application.StartupPath + "\\Log");
	}
	
	//全局异常处理
	Application.ThreadException+=
		new ThreadExceptionEventHandler(Application_ThreadException);
	
	Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
	AppDomain.CurrentDomain.UnhandledException+=
		new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        //...
}

static void ProcessCrash(Exception e) { String path = Application.StartupPath + "\\Log\\test1.exe_" + DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".log"; StreamWriter sw = File.CreateText(path); sw.WriteLine("test1.exe崩溃日志:"); sw.WriteLine("-----------------------------------------------"); sw.WriteLine("[ Error: ]" + e.GetType().ToString()); sw.WriteLine("[ Message: ]" + e.Message); sw.WriteLine("[ Source: ]" + e.Source); sw.WriteLine("[ StackTrace: ]----------------------------\r\n" + e.StackTrace); sw.Flush(); sw.Close(); Application.Restart(); Environment.Exit(0); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Exception exp = e.Exception; MainClass.ProcessCrash(exp); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception exp = (Exception)e.ExceptionObject; MainClass.ProcessCrash(exp); }

本文链接地址: .Net程序的崩溃日志与自动重启
https://blog.qingfengju.com/index.asp?id=317

上一篇: 模拟鼠标清除托盘无效图标
下一篇: 在SQL SERVER2008 R2中创建一个链接到本机的“链接服务器”

分类:Win32/C++ 查看次数:6333 发布时间:2013/2/3 22:14:25