using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace Core.Mes.ServerManager
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                CreateLogDirectory();
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                FrmSeverMain frm = new FrmSeverMain(args); 
                ExitMessageFilter msg = new ExitMessageFilter(frm);
                Application.AddMessageFilter(msg);
                Application.Run(frm);
            }
            catch (Exception ex)
            {

                string path = string.Format(@"{0}/log/Main/Main_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
                StringBuilder sbtxt = new StringBuilder();
                GetException(ex, sbtxt);
                File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);

            }
        }

        private static void CreateLogDirectory()
        {
            string path = string.Format(@"{0}/log/Main/", Application.StartupPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = string.Format(@"{0}/log/Application/", Application.StartupPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = string.Format(@"{0}/log/Method/", Application.StartupPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            string path = string.Format(@"{0}/log/Main/Main_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
            StringBuilder sbtxt = new StringBuilder();
            GetException(ex, sbtxt);
            File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);
        }


        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {

            string path = string.Format(@"{0}/log/Main/ThreadException_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
            StringBuilder sbtxt = new StringBuilder();
            GetException(e.Exception, sbtxt);
            File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);
        }






        public static void GetException(Exception ex, StringBuilder sbtxt)
        {

            if (ex.InnerException != null)
            {
                GetException(ex.InnerException, sbtxt);
            }
            else
            {
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(ex.Message);
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(ex.StackTrace);
                sbtxt.AppendLine("==============================================");
            }
        }
    }
}