gpt4 book ai didi

c# - 这个简单的 Log4Net 包装器可以改进吗?

转载 作者:太空宇宙 更新时间:2023-11-03 19:36:04 26 4
gpt4 key购买 nike

我写了一个简单的 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
}
}

最佳答案

为什么你不能只使用这个:

以下 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>

关于c# - 这个简单的 Log4Net 包装器可以改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1520820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com