/******************************************************************************* * Copyright © 2017-2020 Znyc.Recruitment.Admin.Framework 版权所有 * Author: Znyc * Description: Znyc快速开发平台 * Website:http://www.Znyc.Recruitment.Admin.com *********************************************************************************/ using log4net; using log4net.Config; using log4net.Repository; using System; using System.Collections.Concurrent; using System.Diagnostics; using System.IO; namespace Znyc.Recruitment.Admin.Commons.Log { /// /// log4net封装类 /// *********************************使用说明********************************** /// 1.首先将配置文件(log4net.config或App.config)放置在程序运行目录 /// 2.调用SetConfig方法,并传入配置文件的全路径 /// public class Log4NetHelper { /// /// log4net 仓储库 /// private static ILoggerRepository _repository; private static readonly ConcurrentDictionary Loggers = new ConcurrentDictionary(); /// /// 读取配置文件,并使其生效。如果未找到配置文件,则抛出异常 /// /// /// 配置文件全路径 public static void SetConfig(ILoggerRepository repository, string configFilePath) { _repository = repository; FileInfo fileInfo = new FileInfo(configFilePath); if (!fileInfo.Exists) { throw new Exception("未找到配置文件" + configFilePath); } XmlConfigurator.ConfigureAndWatch(_repository, fileInfo); } /// /// 获取记录器 /// /// soruce /// private static ILog GetLogger(string source) { if (Loggers.ContainsKey(source)) { return Loggers[source]; } else { ILog logger = LogManager.GetLogger(_repository.Name, source); Loggers.TryAdd(source, logger); return logger; } } #region Log a message object /// /// 调试信息日志 /// /// 日志信息 public static void Debug(string msg) { ILog logger = GetLogger("Debug"); if (logger.IsDebugEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } /// /// 错误信息日志 /// /// 日志信息 public static void Error(string msg) { ILog logger = GetLogger("Debug"); if (logger.IsErrorEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } /// /// 异常错误信息日志 /// /// 异常抛出信息 /// 异常信息 public static void Error(string throwMsg, Exception ex) { ILog logger = GetLogger("Error"); if (logger.IsErrorEnabled) { string message = $"抛出信息:{throwMsg} \r\n异常类型:{ex.GetType().Name} \r\n异常信息:{ex.Message} \r\n堆栈调用:\r\n{ex.StackTrace}"; logger.Error(message); } } /// /// 异常错误信息 /// /// source /// 异常抛出信息 /// 异常信息 public static void Error(Type source, object throwMsg, Exception ex) { ILog logger = GetLogger("Error"); if (logger.IsErrorEnabled) { string message = $"抛出信息:{throwMsg} \r\n异常类型:{ex.GetType().Name} \r\n异常信息:{ex.Message} \r\n【堆栈调用】:\r\n{ex.StackTrace}"; logger.Error(message); } } /// /// 关键信息日志 /// /// 日志信息 public static void Info(string msg) { ILog logger = GetLogger("Info"); if (logger.IsInfoEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } /// /// 警告信息日志 /// /// 日志信息 public static void Warn(string msg) { ILog logger = GetLogger("Warn"); if (logger.IsWarnEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } /// /// 失败信息日志 /// /// 日志信息 public static void Fatal(string msg) { ILog logger = GetLogger("Fatal"); if (logger.IsFatalEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } #endregion Log a message object /// /// 关键信息日志 /// /// 文件路径 /// 日志信息 public static void Info(string path, string msg) { ILog logger = GetLogger("Info"); if (logger.IsInfoEnabled) { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); string message = "方法名称:" + methodBase.Name + "\r\n日志内容:" + msg; logger.Info(message); } } } }