using log4net; using log4net.Config; using log4net.Repository; using SteamTool.Core.Properties; using SteamTool.Model; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using System.Xml; namespace SteamTool.Core.Common { /// /// 日志操作管理类 /// public sealed class Logger { private static ILog _textLog; public static bool EnableTextLog = false; static Logger() { ILoggerRepository repository = LogManager.CreateRepository(Assembly.GetCallingAssembly().GetName().Name); if (File.Exists(Path.Combine(AppContext.BaseDirectory, @"log\Log4net.config"))) { XmlConfigurator.Configure(repository, new FileInfo(@"log\Log4net.config")); } else { var doc = new XmlDocument(); doc.LoadXml(Resources.Log4netXml); XmlConfigurator.Configure(repository, doc.DocumentElement); } _textLog = LogManager.GetLogger(repository.Name, "TextLog"); } /// /// 初始化 /// public static void InitLogger(string name, string configFile) { var loggerRepository = LogManager.CreateRepository(name); var path = Path.Combine(AppContext.BaseDirectory, configFile); log4net.Config.XmlConfigurator.Configure(loggerRepository, new FileInfo(path)); _textLog = LogManager.GetLogger(loggerRepository.Name, "TextLog"); } /// /// 毁灭性的错误 /// /// public static void Fatal(object msg) { if (EnableTextLog) { _textLog.Fatal(msg); } } /// /// 毁灭性的错误 /// /// /// public static void Fatal(object msg, Exception ex) { if (EnableTextLog) { _textLog.Fatal(msg, ex); } } /// /// 错误 /// /// public static void Error(object msg) { if (EnableTextLog) { _textLog.Error(msg); } } /// /// 错误 /// /// /// public static void Error(object msg, Exception ex) { if (EnableTextLog) { _textLog.Error(msg, ex); } } /// /// 警告 /// /// public static void Warn(object msg) { if (EnableTextLog) { _textLog.Warn(msg); } } /// /// 警告 /// /// /// public static void Warn(object msg, Exception ex) { if (EnableTextLog) { _textLog.Warn(msg, ex); } } /// /// 消息 /// /// public static void Info(object msg) { if (EnableTextLog) { _textLog.Info(msg); } } /// /// 消息 /// /// /// /// public static void Info(object msg, Exception ex) { if (EnableTextLog) { _textLog.Info(msg, ex); } } /// /// 调试信息 /// /// public static void Debug(object msg) { if (EnableTextLog) { _textLog.Debug(msg); } } /// /// 调试信息 /// /// /// public static void Debug(object msg, Exception ex) { if (EnableTextLog) { _textLog.Debug(msg, ex); } } } }