ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace HotelPms.Share.Windows.Util
{
    public class UnhandledExceptionListener
    {
        public static void Execute()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            if (Application.StartupPath.CompareTo(Environment.CurrentDirectory) != 0) { Environment.CurrentDirectory = Application.StartupPath; }
        }
 
        private static void WriteUnhandledExceptionLog(object data, string title)
        {
            try
            {
                string file = Application.StartupPath + @"\UnhandledException.log";
 
                if (File.Exists(file))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Length > 1024 * 1024) 
                    {
                        string bkfile = string.Format(@"\UnhandledException{0}.log", DateTime.Now.ToString("yyyyMMddHHmmss"));
                        File.Copy(file, bkfile, true);
                        File.Delete(file);
                    }
                }
 
                using (System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Append, System.IO.FileAccess.Write))
                {
                    using (System.IO.StreamWriter w = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8))
                    {
                        w.WriteLine(string.Format("y{0}z{2}F{1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff"), data, title));
                    }
                }
            }
            catch { }
        }
 
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            WriteUnhandledExceptionLog(e.Exception, "Application_ThreadException");
        }
 
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            WriteUnhandledExceptionLog(e.ExceptionObject, "CurrentDomain_UnhandledException" + "F" + e.IsTerminating.ToString());
        }
    }
}