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