using log4net; using System.IO; using System.Reflection; namespace Bro.Common.Log { public class LogHelper { public const string DFT_CONFIG_FILE = "BroC.Comn.Log.BroClog4net.xml"; private static ILog log = null; private static LogHelper inst; /// /// 初始化 /// /// public static LogHelper GetInstance() { if (inst == null) inst = new LogHelper(null); return inst; } /// /// 初始化 /// /// /// public static LogHelper GetInstance(string configPath) { if (inst == null) inst = new LogHelper(configPath); return inst; } /// /// 构造函数 /// /// private LogHelper(string configPath) { var type = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; log = log4net.LogManager.GetLogger(type); // 1. 优先使用手动指定的log4net配置文件 // 2. 不存在手动指定配置文件,则使用默认配置 if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath)) { try { log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath)); } catch { // 默认的log4net配置文件作为内嵌式文件编译进应用集合内 var sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(DFT_CONFIG_FILE); log4net.Config.XmlConfigurator.Configure(sm); } } else { // 默认的log4net配置文件作为内嵌式文件编译进应用集合内 var sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(DFT_CONFIG_FILE); log4net.Config.XmlConfigurator.Configure(sm); } } #region 各级别日志方法 public void Debug(string msg) { log.Debug(msg); } public void DebugFormat(string format, params object[] args) { log.DebugFormat(format, args); } public void Info(string msg) { log.Info(msg); } public void InfoFormat(string format, params object[] args) { log.InfoFormat(format, args); } public void Warn(string msg) { log.Warn(msg); } public void WarnFormat(string format, params object[] args) { log.WarnFormat(format, args); } public void Error(string msg) { log.Error(msg); } public void ErrorFormat(string format, params object[] args) { log.ErrorFormat(format, args); } public void Fatal(string msg) { log.Fatal(msg); } public void FatalFormat(string format, params object[] args) { log.FatalFormat(format, args); } #endregion } }