using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IStation { public class LogHelper { /// /// 初始化 /// public static void Initial() { var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"); log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(path)); } /// /// 初始化 (Web调用) /// public static void InitialWeb() { var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "log4net.config"); log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(path)); } #region Info //这里的 loginfo 和 log4net.config 里的名字要一样 private static readonly log4net.ILog _loginfo = log4net.LogManager.GetLogger("loginfo"); /// /// 写入信息日志 /// /// public static void Info(string info) { if (_loginfo.IsInfoEnabled) { _loginfo.Info(info); } } /// /// 写入信息日志 /// /// /// public static void InfoFormat(string format, params object[] args) { if (_loginfo.IsInfoEnabled) { _loginfo.InfoFormat(format, args); } } #endregion #region Debug //这里的 logdebug 和 log4net.config 里的名字要一样 private static readonly log4net.ILog _logdebug = log4net.LogManager.GetLogger("logdebug"); /// /// 写入调试日志 /// /// public static void Debug(string info) { if (_logdebug.IsInfoEnabled) { _logdebug.Debug(info); } } /// /// 写入调试日志 /// /// /// public static void DebugFormat(string format, params object[] args) { if (_logdebug.IsInfoEnabled) { _logdebug.DebugFormat(format, args); } } #endregion #region Error //这里的 logerror 和 log4net.config 里的名字要一样 private static readonly log4net.ILog _logerror = log4net.LogManager.GetLogger("logerror"); /// /// 写入错误日志 /// /// /// public static void Error(string info, Exception ex = null) { if (_logerror.IsErrorEnabled) { if (ex == null) _logerror.Error(info); else _logerror.Error(info, ex); } } /// /// 写入错误日志 /// /// /// public static void ErrorFormat(string format, params object[] args) { if (_logerror.IsErrorEnabled) { _logerror.ErrorFormat(format, args); } } /// /// 写入错误日志 /// /// /// 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 } }