这个简单的Log4Net包装器可以改进吗?
我写了一个简单的log4net包装器。 我想知道这个包装器代码是否可以改进。
我有点担心每个日志function(信息,警告等)中引入的reflection代码以获取调用函数名称。 是否可能出现任何可能的性能问题?
namespace Acqueon.Pacer.Core.Helpers { #region Imports using System; using System.Diagnostics; using System.Reflection; using log4net; #endregion /// /// log4net Log helper /// public sealed class Logger { #region Constants and Fields /// /// Determines whether the DEBUG Mode is enabled. /// private readonly bool isDebugEnabled; /// /// The is error enabled. /// private readonly bool isErrorEnabled; /// /// Determines whether the FATAL Mode is enabled. /// private readonly bool isFatalEnabled; /// /// Determines whether the INFO Mode is enabled. /// private readonly bool isInfoEnabled; /// /// Determines whether the WARN Mode is enabled. /// private readonly bool isWarnEnabled; /// /// The logger object /// private readonly ILog log; #endregion #region Constructors and Destructors /// /// Initializes a new instance of the Logger class. /// public Logger() : this(new StackTrace().GetFrame(1).GetMethod().DeclaringType) { } /// /// Initializes a new instance of the Logger class. /// /// /// The type of logger. /// public Logger(Type type) { this.log = LogManager.GetLogger(type); this.isDebugEnabled = this.log.IsDebugEnabled; this.isErrorEnabled = this.log.IsErrorEnabled; this.isInfoEnabled = this.log.IsInfoEnabled; this.isFatalEnabled = this.log.IsFatalEnabled; this.isWarnEnabled = this.log.IsWarnEnabled; } #endregion #region Public Methods /// /// Logs the debug message. /// /// /// The message. /// public void Debug(string message) { if (this.isDebugEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Debug(methodBase.Name + " : " + message); } } /// /// Logs the debug message and the exception. /// /// /// The message. /// /// /// The exception. /// public void Debug(string message, Exception exception) { if (this.isDebugEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Debug(methodBase.Name + " : " + message, exception); } } /// /// Logs the error message. /// /// /// The error message. /// public void Error(string errorMessage) { if (this.isErrorEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Error(methodBase.Name + " : " + errorMessage); } } /// /// Logs the error message and the exception. /// /// /// The error message. /// /// /// The exception. /// public void Error(string errorMessage, Exception exception) { if (this.isErrorEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Error(methodBase.Name + " : " + errorMessage, exception); } } /// /// Logs the fatal error message. /// /// /// The message. /// public void Fatal(string message) { if (this.isFatalEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Fatal(methodBase.Name + " : " + message); } } /// /// Logs the fatal error message and the exception. /// /// /// The message. /// /// /// The exception. /// public void Fatal(string message, Exception exception) { if (this.isFatalEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Fatal(methodBase.Name + " : " + message, exception); } } /// /// Logs the info message. /// /// /// The message. /// public void Info(string message) { if (this.isInfoEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Info(methodBase.Name + " : " + message); } } /// /// Logs the info message and the exception. /// /// /// The message. /// /// /// The exception. /// public void Info(string message, Exception exception) { if (this.isInfoEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Info(methodBase.Name + " : " + message, exception); } } /// /// Logs the warning message. /// /// /// The message. /// public void Warn(string message) { if (this.isWarnEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Warn(methodBase.Name + " : " + message); } } /// /// Logs the warning message and the exception. /// /// /// The message. /// /// /// The exception. /// public void Warn(string message, Exception exception) { if (this.isWarnEnabled) { MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Warn(methodBase.Name + " : " + message, exception); } } #endregion } }
你为什么不能用它:
以下PatternLayout模式提取位置信息:
%F用于输出发出日志记录请求的文件名
%L用于输出发出日志记录请求的行号
%M用于输出发出日志记录请求的方法名称
%C用于输出发出日志记录请求的调用方的完全限定类名。
请注意,在两种情况下都需要堆叠步行,这是昂贵的。
reflection代码最终会受到影响,显然取决于您在整个应用程序中记录的日志消息数量。
您还可以使用正确的转换模式输出行号,基本调用堆栈和方法信息。 默认情况下,此function存在于log4net中 – 文档警告您存在性能损失。
最后,我将在Exception上实现您的包装类作为扩展方法。