/// <summary> /// 打印Info日志 /// </summary> /// <param name="format">日志格式串</param> /// <param name="param">日志参数</param> public void Info(string format, params object[] param) { try { if (param == null || param.Length == 0) { _logger.Info(format); } else { _logger.InfoFormat(format, param); } } catch (Exception ex) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { Console.WriteLine("Logger Info Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace); } try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Logger Info Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace), System.Windows.Forms.Application.ProductName); } catch { } } }
/// <summary> /// 运行服务 /// </summary> public void RunService(bool hadInterval) { try { int join0num = 0; if (hadInterval == true) { while (_Service.IsRunning /* || Network.NetworkManager.Instance.CheckNetworkThreadIsAlive()*/) { int startTick = Environment.TickCount; m_stopwatch.Restart(); _Service.Update(); m_stopwatch.Stop(); if (m_stopwatch.Elapsed < m_maxSleepTS) { Thread.Sleep(m_maxSleepTS - m_stopwatch.Elapsed); } else { Thread.Sleep(1); } } } else { while (_Service.IsRunning /* || Network.NetworkManager.Instance.CheckNetworkThreadIsAlive()*/) { m_stopwatch.Restart(); _Service.Update(); m_stopwatch.Stop(); if (m_stopwatch.Elapsed < m_maxSleepTS) { Thread.Sleep(m_maxSleepTS - m_stopwatch.Elapsed); } else { Thread.Sleep(1); } } } _Service.ExitInstance(); } catch (Exception e) { Console.WriteLine("Abnormal Application Termination :{0}\r\nStackTrace : \r\n{1}", e.Message, e.StackTrace); try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Abnormal Application Termination : {0}\r\nStackTrace : \r\n{1}", e.Message, e.StackTrace), _Service.GetAssemblyName()); } catch { } } }
/// <summary> /// 初始化日志 /// </summary> /// <param name="file">日志文件</param> /// <param name="datePattern">日期格式</param> /// <param name="fileLevelStr">文件日志等级,有debug、info、warn、error和fatal</param> /// <param name="consoleLevelStr">Console日志等级</param> /// <returns>返回是否成功</returns> public bool Initialize(string file, string datePattern, string fileLevelStr, string consoleLevelStr) { log4net.Core.Level fileLevel = ConvertLog4NetLevel(fileLevelStr); log4net.Core.Level consoleLevel = ConvertLog4NetLevel(consoleLevelStr); const string DefaultPattern = "%d [%t]%-5p %c - %m%n"; try { log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(DefaultPattern); if (file != string.Empty) { log4net.Appender.RollingFileAppender rfileAppender = new log4net.Appender.RollingFileAppender(); rfileAppender.Layout = layout; rfileAppender.File = file; if (datePattern == string.Empty) { rfileAppender.DatePattern = "yyyyMMdd-HH"; } else { rfileAppender.DatePattern = datePattern; } rfileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; //.Composite; rfileAppender.Threshold = fileLevel; rfileAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(rfileAppender); } log4net.Appender.ConsoleAppender consoleAppender = new log4net.Appender.ConsoleAppender(layout); consoleAppender.Threshold = consoleLevel; consoleAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(consoleAppender); return(true); } catch (Exception err) { Console.WriteLine("Error:{0} StackTrace:{1}", err.Message, err.StackTrace); try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Error : {0}\r\nStackTrace : \r\n{1}", err.Message, err.StackTrace), System.Windows.Forms.Application.ProductName); } catch { } return(false); } }
/// <summary> /// 构造 /// </summary> /// <param name="service">服务类</param> /// <param name="args">服务参数</param> public BasicServiceRunner(BasicService service, string[] args) { #region 去掉关闭菜单 try { int hwnd = (int)System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; int hmenu = PInvoker.GetSystemMenu(hwnd, false); bool bsucc = PInvoker.RemoveMenu(hmenu, 0xF060, 0); } catch { } #endregion #region 初始化 try { _Service = service; _Service.InitInstance(args); _Service.IsRunning = true; } catch (Exception e) { Console.WriteLine("Abnormal Application Termination :{0}\r\nStackTrace : \r\n{1}", e.Message, e.StackTrace); try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Abnormal Application Termination : {0}\r\nStackTrace : \r\n{1}", e.Message, e.StackTrace), _Service.GetAssemblyName()); } catch { } //Network.NetworkManager.Instance.StopRunNetworkUpdate(); _Service.IsRunning = false; return; } #endregion }
/// <summary> /// 打印Fatal日志 /// </summary> /// <param name="err">异常对象</param> /// <param name="description">错误描述</param> /// <param name="param">错误参数</param> public void Fatal(Exception err, string description, params object[] param) { try { _logger.Fatal(string.Format(description, param), err); } catch (Exception ex) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { Console.WriteLine("Logger Fatal Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace); } try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Logger Fatal Exception : {0}\r\nStackTrace : \r\n{1}", ex.Message, ex.StackTrace), System.Windows.Forms.Application.ProductName); } catch { } } }