Winform 마스터 포털에 글로벌 예외 기록 및 캡처
6428 단어 C# 학습 기록
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
LogHepler log = new LogHepler();
try
{
//
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// UI
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// UI
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region Program.cs
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
CultureInfo ci = new CultureInfo("zh-hans");
Application.CurrentCulture = ci;
DevExpress.UserSkins.BonusSkins.Register();
DevExpress.Skins.SkinManager.EnableMdiFormSkins();
UserLookAndFeel.Default.SetSkinStyle(Properties.Settings.Default.Theme);
DevExpress.Skins.SkinManager.EnableFormSkins();
DialogResult result = DialogResult.None;
using (frmLogin fl = new frmLogin())
{
result = fl.ShowDialog();
if (result == DialogResult.OK)
{
SplashScreenManager.ShowForm(null, typeof(frmSplash), true, true, false, 1000);
for (int i = 1; i <= 100; i++)
{
SplashScreenManager.Default.SendCommand(frmSplash.SplashScreenCommand.SetProgress, i);
Thread.Sleep(30);
}
SplashScreenManager.CloseForm(false);
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
#endregion
}
catch (Exception ex)
{
string str = "";
string strDateInfo = " :" + DateTime.Now.ToString() + "\r
";
if (ex != null)
{
str = string.Format(strDateInfo + " :{0}\r
:{1}\r
:{2}\r
",
ex.GetType().Name, ex.Message, ex.StackTrace);
}
else
{
str = string.Format(" :{0}", ex);
}
MessageBox.Show(str, " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
log.WriteLog(str);
}
}
#region
///
///
///
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
LogHepler log = new LogHepler();
string str = "";
string strDateInfo = " :" + DateTime.Now.ToString() + "\r
";
Exception error = e.Exception as Exception;
if (error != null)
{
str = string.Format(strDateInfo + " :{0}\r
:{1}\r
:{2}\r
",
error.GetType().Name, error.Message, error.StackTrace);
}
else
{
str = string.Format(" :{0}", e);
}
DevExpress.XtraEditors.XtraMessageBox.Show(UserLookAndFeel.Default, error.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
//MessageBox.Show(error.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
log.WriteLog(str);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
LogHepler log = new LogHepler();
string str = "";
Exception error = e.ExceptionObject as Exception;
string strDateInfo = " :" + DateTime.Now.ToString() + "\r
";
if (error != null)
{
str = string.Format(strDateInfo + "Application UnhandledException:{0};
\r :{1}", error.Message, error.StackTrace);
}
else
{
str = string.Format("Application UnhandledError:{0}", e);
}
MessageBox.Show(error.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
log.WriteLog(str);
}
#endregion
}
로그 도움말 클래스public class LogHepler
{
///
///
///
private static object lockHelper = new object();
///
/// error
///
///
///
public void WriteErrorLog(string errorMessage, Exception ex)
{
string errorMsg = string.Empty;
if (ex.InnerException != null)
{
errorMsg = ex.InnerException.Message;
}
errorMsg = errorMsg + errorMessage + ex.StackTrace;
WriteLog(errorMsg);
}
///
///
///
///
public void WriteLog(string msg)
{
lock (lockHelper)
{
string FilePath = string.Empty;
string AbsolutePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log";
if (!Directory.Exists(AbsolutePath))
{
Directory.CreateDirectory(AbsolutePath);
}
FilePath = AbsolutePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
File.AppendAllText(FilePath, "\r
" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r
" + msg, Encoding.GetEncoding("gb2312"));
}
}
}