static ILogNet errorLogNet = new LogNetDateTime(Application.StartupPath + "\\Logs\\窗体和线程异常日志", GenerateMode.ByEveryDay); // 创建日志器,按每天存储不同的文件 private static void UIThreadException(object sender, ThreadExceptionEventArgs t) { try { string errorMsg = "Windows窗体线程异常 : \r\n"; errorLogNet.WriteDebug(errorMsg + t.Exception.Message + Environment.NewLine + t.Exception.StackTrace); } catch { errorLogNet.WriteDebug("不可恢复的Windows窗体异常,应用程序将退出!"); } finally { //MessageBox.Show("不可恢复的Windows窗体异常!"); //发生异常后的两种处理方式 //一、重新启动应用程序 sysLog.WriteDebug("不可恢复的Windows窗体异常,应用程序将重启!"); Program.Run.Close(); Application.Restart(); //二、退出应用程序 //Program.sysLog.WriteAnyString("不可恢复的Windows窗体异常,应用程序将退出!"); //Application.Exit(); ////日志记录必须放在环境退出之前 //Environment.Exit(0); } }
//static void Main() //{ // Application.EnableVisualStyles(); // Application.SetCompatibleTextRenderingDefault(false); // //Thread.Sleep(5000); // Application.Run(new RuChuKuForm()); //} static void Main() { bool noRun = false; Run = new System.Threading.Mutex(true, "HumControl", out noRun); //检测是否已经运行 if (noRun)//未运行 { try { Run.ReleaseMutex(); Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); sysLog.WriteAnyString("---------------------------------------------------------------------------------"); sysLog.WriteDebug("系统开始启动!"); Application.Run(new RuChuKuForm()); } catch { Application.Restart(); } } else//已经运行 { //MessageBox.Show("系统正在运行\r\n请不要重复开启!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //MessageBox.Show("已经有一个实例正在运行!"); //切换到已打开的实例 } }
public static void WriteMsg(MessageType degree, object obj, string strMsg, string strMark = "", Exception ex = null) { string strKey = (obj != null ? obj.GetType().FullName : "") + (string.IsNullOrEmpty(strMark) ? "" : " Mark:" + strMark); switch (degree) { case MessageType.DEBUG: logNet.WriteDebug(strKey, strMsg); break; case MessageType.ERROR: logNet.WriteError(strKey, strMsg); break; case MessageType.FATAL: logNet.WriteFatal(strKey, strMsg); break; case MessageType.INFO: logNet.WriteInfo(strKey, strMsg); break; case MessageType.NONE: logNet.WriteDescrition(strMsg); break; case MessageType.WARN: logNet.WriteWarn(strKey, strMsg); break; case MessageType.EXCEPTION: logNet.WriteException(strKey, ex); break; } }
ILogNet logNetTime = new LogNetDateTime(Application.ResourceAssembly + "\\LogByTime", GenerateMode.ByEveryDay);//按每天 private void TestButton_Click(object sender, RoutedEventArgs e) { // 一般日志写入 logNet.WriteDebug("调试信息"); logNet.WriteInfo("一般信息"); logNet.WriteWarn("警告信息"); logNet.WriteError("错误信息"); logNet.WriteFatal("致命信息"); logNet.WriteException(null, new IndexOutOfRangeException()); // 带有关键字的写入,关键字建议为方法名或是类名,方便分析的时候归类搜索 logNet.WriteDebug("userButton1_Click", "调试信息"); logNet.WriteInfo("TestForm", "一般信息"); logNet.WriteWarn("随便什么", "警告信息"); logNet.WriteError("userButton1_Click", "错误信息"); logNet.WriteFatal("userButton1_Click", "致命信息"); logNet.WriteException("userButton1_Click", new IndexOutOfRangeException()); // 日志查看器 using (HslCommunication.LogNet.FormLogNetView form = new HslCommunication.LogNet.FormLogNetView()) { form.ShowDialog(); } }
private void InitSocket() { try { if (!m_socketScan.Connected) { m_socketScan.Connect(endPoint); } m_scanRun = true; LogNetProgramer.WriteInfo("连接扫描枪成功!"); AddTips("连接扫描枪成功!"); } catch (Exception ex) { LogNetProgramer.WriteDebug("异常:" + ex.StackTrace + "--->" + ex.Message); MessageBox.Show(ex.Message); } }
public static void WriteDebugLog(string info) { LogNet.WriteDebug(info); }