| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | |
| | | namespace IStation |
| | | { |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | public class LogHelper |
| | | { |
| | | public static void Initial() |
| | | { |
| | | string directory ; |
| | | if (IStation.DataFolderHelper.IsExeExcute) |
| | | { |
| | | directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).FullName; |
| | | } |
| | | else |
| | | { |
| | | directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| | | } |
| | | var configFile = System.IO.Path.Combine(directory, "Config","log4net.config"); |
| | | #region Info |
| | | |
| | | if (System.IO.File.Exists(configFile)) |
| | | { |
| | | log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configFile)); |
| | | } |
| | | else |
| | | { |
| | | log4net.Config.XmlConfigurator.Configure(); |
| | | } |
| | | private static string InfoName = "Info"; |
| | | |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logInfo = log4net.LogManager.GetLogger("IStation.Info"); |
| | | logInfo.Info("初始化成功"); |
| | | } |
| | | //这里的 loginfo 和 log4net.config 里的名字要一样 |
| | | private static readonly log4net.ILog _loginfo = LogConfig.GetLogger(InfoName); |
| | | |
| | | #region 错误日志 |
| | | private static log4net.ILog logError = null; |
| | | public static void WriteError(string msg,Exception ex) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(msg, ex); |
| | | } |
| | | public static void WriteError(string msg ,int line, Exception ex) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(string.Format("msg:{0}, line:{1}",msg,line), ex); |
| | | } |
| | | public static void WriteError(string msg) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(msg); |
| | | } |
| | | public static void Error( Exception ex) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(ex.Message, ex); |
| | | } |
| | | public static void Error(string msg, Exception ex) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(msg, ex); |
| | | } |
| | | public static void Error(string msg, int line, Exception ex) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(string.Format("msg:{0}, line:{1}", msg, line), ex); |
| | | } |
| | | public static void Error(string msg) |
| | | { |
| | | if (logError == null) |
| | | logError = log4net.LogManager.GetLogger("IStation.Error"); |
| | | logError.Error(msg); |
| | | } |
| | | #endregion |
| | | |
| | | #region 正常日志 |
| | | private static log4net.ILog logInfo = null; |
| | | /// <summary> |
| | | /// 写入信息日志 |
| | | /// </summary> |
| | | public static void Info(string info) |
| | | { |
| | | if (logInfo == null) |
| | | logInfo = log4net.LogManager.GetLogger("IStation.Info"); |
| | | if (logInfo != null) |
| | | if (_loginfo.IsInfoEnabled) |
| | | { |
| | | logInfo.Info(info); |
| | | _loginfo.Info(info); |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | /// <summary> |
| | | /// 写入信息日志 |
| | | /// </summary> |
| | | public static void InfoFormat(string format, params object[] args) |
| | | { |
| | | if (_loginfo.IsInfoEnabled) |
| | | { |
| | | _loginfo.InfoFormat(format, args); |
| | | } |
| | | } |
| | | |
| | | #endregion Info |
| | | |
| | | #region Debug |
| | | |
| | | private static string DebugName = "Debug"; |
| | | |
| | | //这里的 logdebug 和 log4net.config 里的名字要一样 |
| | | private static readonly log4net.ILog _logdebug = LogConfig.GetLogger(DebugName); |
| | | |
| | | /// <summary> |
| | | /// 写入调试日志 |
| | | /// </summary> |
| | | public static void Debug(string info) |
| | | { |
| | | if (_logdebug.IsInfoEnabled) |
| | | { |
| | | _logdebug.Debug(info); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 写入调试日志 |
| | | /// </summary> |
| | | public static void DebugFormat(string format, params object[] args) |
| | | { |
| | | if (_logdebug.IsInfoEnabled) |
| | | { |
| | | _logdebug.DebugFormat(format, args); |
| | | } |
| | | } |
| | | |
| | | #endregion Debug |
| | | |
| | | #region Error |
| | | |
| | | private static string ErrorName = "Error"; |
| | | |
| | | private static readonly log4net.ILog _logerror = LogConfig.GetLogger(ErrorName); |
| | | |
| | | /// <summary> |
| | | /// 写入错误日志 |
| | | /// </summary> |
| | | public static void Error(string info, Exception ex = null) |
| | | { |
| | | if (_logerror.IsErrorEnabled) |
| | | { |
| | | if (ex == null) |
| | | _logerror.Error(info); |
| | | else |
| | | _logerror.Error(info, ex); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 写入错误日志 |
| | | /// </summary> |
| | | public static void ErrorFormat(string format, params object[] args) |
| | | { |
| | | if (_logerror.IsErrorEnabled) |
| | | { |
| | | _logerror.ErrorFormat(format, args); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 写入错误日志 |
| | | /// </summary> |
| | | public static void Error(string info, int line, Exception ex = null) |
| | | { |
| | | if (_logerror.IsErrorEnabled) |
| | | { |
| | | if (ex == null) |
| | | _logerror.Error(info + " 在代码第" + line); |
| | | else |
| | | _logerror.Error(info + " 在代码第" + line, ex); |
| | | } |
| | | } |
| | | |
| | | #endregion Error |
| | | |
| | | #region 自定义 |
| | | |
| | | /// <summary> |
| | | /// 自定义 |
| | | /// </summary> |
| | | public static void Custom(string name, string msg) |
| | | { |
| | | var log = LogConfig.GetLogger(name); |
| | | if (log.IsInfoEnabled) |
| | | { |
| | | log.Info(msg); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 自定义 |
| | | /// </summary> |
| | | public static void CustomFormat(string name, string format, params object[] args) |
| | | { |
| | | var log = LogConfig.GetLogger(name); |
| | | if (log.IsInfoEnabled) |
| | | { |
| | | log.InfoFormat(format, args); |
| | | } |
| | | } |
| | | |
| | | #endregion 自定义 |
| | | } |
| | | } |