可这个简单的log4net的包装改进? [英] Can This Simple Log4Net Wrapper Be Improved?

查看:178
本文介绍了可这个简单的log4net的包装改进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的log4net的包装。我想知道这个包装code是否可以改进。

我是有点担心的反映code抛出到每个记录功能(信息,警告等),以获取调用函数名。是否有可能是由于这种任何可能的性能问题?

 命名空间Acqueon.Pacer.Core.Helpers
{
    #地区进口

    使用系统;
    使用System.Diagnostics程序;
    使用的System.Reflection;

    使用log4net的;

    #endregion

    ///<总结>
    /// log4net的日志帮手
    ///< /总结>
    公共密封类记录仪
    {
        #地区的常量和字段

        ///<总结>
        ///确定调试模式是否启用。
        ///< /总结>
        私人只读布尔isDebugEnabled;

        ///<总结>
        ///的是启用的错误。
        ///< /总结>
        私人只读布尔isErrorEnabled;

        ///<总结>
        ///确定致命的模式是否启用。
        ///< /总结>
        私人只读布尔isFatalEnabled;

        ///<总结>
        ///确定信息模式是否启用。
        ///< /总结>
        私人只读布尔isInfoEnabled;

        ///<总结>
        ///确定是否WARN模式已启用。
        ///< /总结>
        私人只读布尔isWarnEnabled;

        ///<总结>
        /// Logger对象
        ///< /总结>
        私人只读的ILog记录;

        #endregion

        #地区的构造和析构

        ///<总结>
        ///初始化Logger类的新实例。
        ///< /总结>
        公共记录仪()
            :这(新的堆栈跟踪()的getFrame(1).GetMethod()DeclaringType)
        {
        }

        ///<总结>
        ///初始化Logger类的新实例。
        ///< /总结>
        ///< PARAM NAME =输入>
        ///记录器的类型。
        ///< /参数>
        公共记录仪(型号类型)
        {
            this.log = LogManager.GetLogger(类型);

            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

        #地区的公共方法

        ///<总结>
        ///日志的调试消息。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        公共无效调试(字符串消息)
        {
            如果(this.isDebugEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Debug(methodBase.Name +:+消息);
            }
        }

        ///<总结>
        ///日志的调试消息和异常。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        ///< PARAM NAME =异常>
        ///的异常。
        ///< /参数>
        公共无效调试(字符串消息,异常除外)
        {
            如果(this.isDebugEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Debug(methodBase.Name +:+消息除外);
            }
        }

        ///<总结>
        ///记录错误消息。
        ///< /总结>
        ///< PARAM NAME =的errorMessage>
        ///错误消息。
        ///< /参数>
        公共无效的错误(字符串的errorMessage)
        {
            如果(this.isErrorEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Error(methodBase.Name +:+的errorMessage);
            }
        }

        ///<总结>
        ///记录错误消息和异常。
        ///< /总结>
        ///< PARAM NAME =的errorMessage>
        ///错误消息。
        ///< /参数>
        ///< PARAM NAME =异常>
        ///的异常。
        ///< /参数>
        公共无效的错误(字符串的errorMessage,例外的例外)
        {
            如果(this.isErrorEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Error(methodBase.Name +:+的errorMessage,除外);
            }
        }

        ///<总结>
        ///日志的致命错误消息。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        公共无效致命(字符串消息)
        {
            如果(this.isFatalEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Fatal(methodBase.Name +:+消息);
            }
        }

        ///<总结>
        ///日志的致命错误消息和异常。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        ///< PARAM NAME =异常>
        ///的异常。
        ///< /参数>
        公共无效致命(字符串消息,异常除外)
        {
            如果(this.isFatalEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Fatal(methodBase.Name +:+消息除外);
            }
        }

        ///<总结>
        ///日志的信息消息。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        公共无效信息(字符串消息)
        {
            如果(this.isInfoEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Info(methodBase.Name +:+消息);
            }
        }

        ///<总结>
        ///日志的信息消息和异常。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        ///< PARAM NAME =异常>
        ///的异常。
        ///< /参数>
        公共无效信息(字符串消息,异常除外)
        {
            如果(this.isInfoEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Info(methodBase.Name +:+消息除外);
            }
        }

        ///<总结>
        ///日志的警告信息。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        公共无效的警告(字符串消息)
        {
            如果(this.isWarnEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Warn(methodBase.Name +:+消息);
            }
        }

        ///<总结>
        ///日志警告消息和异常。
        ///< /总结>
        ///< PARAM NAME =消息>
        ///消息。
        ///< /参数>
        ///< PARAM NAME =异常>
        ///的异常。
        ///< /参数>
        公共无效的警告(字符串消息,异常除外)
        {
            如果(this.isWarnEnabled)
            {
                MethodBase methodBase =新的堆栈跟踪()的getFrame(1).GetMethod()。
                this.log.Warn(methodBase.Name +:+消息除外);
            }
        }

        #endregion
    }
}
 

解决方案

为什么你就不能使用这样的:

下面的PatternLayout图案提取的位置信息:

%F用于输出发出日志请求所在的文件名

%L用来输出从那里日志请求是行​​号 印发

%M用于输出发出日志请求所在的方法名

%C用于输出呼叫者发出的完全限定类名 记录的要求。

请注意,在这两种情况下,堆叠的步行是必需的,这是昂贵的

 <附加目的地名称=DEBUGOUT
         TYPE =log4net.Appender.OutputDebugStringAppender>
 <布局类型=log4net.Layout.PatternLayout>
   < conversionPattern值=% -  5P [%T]%C {1}%M  - %M%N。/>
 < /布局>
 

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

解决方案

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>

这篇关于可这个简单的log4net的包装改进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆