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

发布于 2024-08-06 11:55:17 字数 7766 浏览 6 评论 0原文

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

I have written a simple log4net wrapper. I was wondering whether this wrapper code could be improved.

I am little bit worried about the reflection code thrown in into each Logging Function (Info, Warn etc) to get the Calling function name. Whether there could be any possible performance problems in due to this?

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-08-13 11:55:17

为什么不能直接使用这个:

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

Why can't you just use this:

The following PatternLayout patterns extract location information:

%F Used to output the file name where the logging request was issued

%L Used to output the line number from where the logging request was
issued

%M Used to output the method name where the logging request was issued

%C Used to output the fully qualified class name of the caller issuing the
logging request.

Please note that in both cases stack walk is required, which is expensive.

<appender name="DebugOut"
         type="log4net.Appender.OutputDebugStringAppender">
 <layout type="log4net.Layout.PatternLayout">
   <conversionPattern value="%-5p [%t] %C{1}.%M - %m%n" />
 </layout>

世界等同你 2024-08-13 11:55:17

反射代码最终会对性能造成影响,这显然取决于您在整个应用程序中记录的日志消息数量。

您还可以使用正确的转换模式输出行号、基本调用堆栈和方法信息。默认情况下,log4net 中存在此功能 - 并且文档警告您性能会受到影响。

最后,我将实现您的包装类作为异常的扩展方法。

There will eventually be a performance hit from the reflection code, obviously depending on how many log messages you are logging throughout your application.

You can also already output line number, a basic call stack and method information using the correct conversion pattern. This functionality is present in log4net by default - and the docs warn you that there is a performance hit.

Lastly, I would implement your wrapper class as extension methods on Exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文