您的位置:首页 > 其它

Debug技巧-------控制台程序或者应用程序输出错误到txt

2013-07-12 17:40 239 查看
控制台程序在服务器上运行错误时容易崩溃,无法找到原因

为了找到原因 我们可以 把 错误输出 在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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐