難免程式交付到客戶會因為缺少某些環境造成程式無法執行
開發者大多會想知道錯誤訊息,當然要知道錯誤訊息也可以透過事件擷取器
不過正常來說不會請客戶回傳擷取事件紀錄器中的紀錄所以可以透過UnhandledException事件去產生記錄檔並請客戶回傳該紀錄檔
下方就是範例程式碼:
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace TestForm
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LineAutoSendMessageForm());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
File.AppendAllText("log.txt", e.ExceptionObject.ToString(), Encoding.UTF8);
}
catch
{
}
}
}
}
執行結果:

