Program.cs
3.36 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Hh.Mes.Common.log;
using log4net;
using log4net.Config;
namespace HH.Data.Excel
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
AppLicationExceptionInit();
InitLog4Net();
AppStart();
}
static void AppStart()
{
Mutex mutex = new Mutex(true, Application.ProductName, out bool MoreAppFalg);
if (MoreAppFalg)
{
Application.Run(new Form1());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "相同的程序已经在运行了,请不要同时运行多个程序!\n\n当前程序即将退出!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
#region 全局异常处理
static void AppLicationExceptionInit()
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//ThreadException 处理UI线程异常
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
//UnhandledException 处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// 处理非UI线程的异常
Log4NetHelper.Instance.Error(e.ToString());
MessageBox.Show("发生了一个非UI线程异常。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// 处理UI线程的异常
Exception ex = (Exception)e.ExceptionObject;
Log4NetHelper.Instance.Error(ex.ToString());
MessageBox.Show("发生了一个UI线程异常。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
#endregion
#region Log4Net
private static void InitLog4Net()
{
Log4NetHelper.Instance.Repository = LogManager.CreateRepository("NETCoreRepository");
var log4ConfigFilePath = Path.Combine(
Path.GetDirectoryName(typeof(Program).Assembly.Location) ?? string.Empty,
"config",
"log4net.config"
);
if (!File.Exists(log4ConfigFilePath))
{
Console.ForegroundColor = ConsoleColor.Red;
SysMsgShow("log4net.config文件路径不存在,请确认配置文件路径是否正确!");
Log4NetHelper.Instance.Info("InitLog4Net Init Failed");
}
else
{
XmlConfigurator.Configure(Log4NetHelper.Instance.Repository, new FileInfo(log4ConfigFilePath));
Log4NetHelper.Instance.Info("InitLog4Net Init Started");
}
}
#endregion
/// <summary>
/// 消息提醒 IOT-数据采集地址协议解析
/// </summary>
public static void SysMsgShow(string msg)
{
MessageBox.Show(msg, "IOT-数据采集地址协议解析");
}
}
}