设置log4net的记录器的名称 [英] Setting the name of a log4net logger

查看:97
本文介绍了设置log4net的记录器的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序(MVC3 Web应用程序)与log4net的城堡记录工具。然而,而不是使用ILogger接口直接我已经有一个具体的实现Log4NetAuditor创建另一个接口(IAuditor)(见code以下)。这个抽象

I am using the Castle logging facility with log4net in my application (MVC3 web app). However, rather than using the ILogger interface directly I have abstracted this by creating another interface (IAuditor) with a concrete implementation Log4NetAuditor (see code below).

您可能会问,为什么我做这个,因为ILogger点是抽象的底层日志实现。我已经做了,因为洋葱建筑原则的严格观察抽象的所有基础设施问题。

You may ask why I've done this since the point of ILogger is to abstract the underlying logging implementation. I've done because of strict observation of the onion architecture principle of abstracting all infrastructure concerns.

现在,这一切除了记录仪同名作品就好。

Now, this works just fine except all loggers are named the same: MyProject.Infrastructure.Log4NetAuditor:

2011-01-24 13:26:11,746 [11] DEBUG MyProject.Infrastructure.Log4NetAuditor [] - Attempting login...
2011-01-24 13:26:11,845 [11] DEBUG MyProject.Infrastructure.Log4NetAuditor [] - Authentication result is Authenticated

的IAuditor露出作为一个AuditableComponent它是一个抽象类从该需要记录所有组件派生属性

The IAuditor is exposed as a property on an AuditableComponent which is an abstract class from which all components that require logging derive.

public abstract class AuditableComponent
{
    private IAuditor _auditor = NullAuditor.Instance;

    public IAuditor Auditor
    {
        get { return _auditor; }
        set { _auditor = value; }
    }

    //....
}

该Log4NetAuditor类看起来是这样的:

The Log4NetAuditor class looks like this:

public class Log4NetAuditor : IAuditor
{
    private ILogger _logger = NullLogger.Instance;

    public ILogger Logger
    {
        get { return _logger; }
        set { _logger = value; }
    }

    public void Trace(string message)
    {
        _logger.Debug(message);
    }
}

现在,很明显,为什么所有的日志报表有相同的记录器名称:ILogger是在Log4NetAuditor类注射

Now, it's clear why all log statements have the same logger name: ILogger is injected on the Log4NetAuditor class.

那么,如何能保证我的记录器的名称对应于扩展AuditableComponent类的名字吗?

How, then, can I ensure the name of the logger corresponds to the name of the class that extends AuditableComponent?

推荐答案

我做同样的事情,你,但我也包含一个静态的LogManager类是谁的工作它是实例化记录。我做我的构造函数的Logger类(你的审计类)内部,使他们不能直接创建。这意味着,任何外部实体都需要打通LogManager类的引用。

I did the same thing as you but I also included a static LogManager class who's job it was to instantiate your logger. I made my constructors internal on the Logger class (your auditor class) so that they could not be created directly. This means that any external entity would need to get their reference through the LogManager class.

public static class LogManager
{
    public static readonly ILogger Log = CreateLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    /// <summary>
    /// Creates the logger for the specified type.
    /// </summary>
    /// <param name="loggerType">Type of the logger.</param>
    /// <returns></returns>
    public static Logger CreateLogger(Type loggerType)
    {
        return new Logger(loggerType);
    }

    /// <summary>
    /// Creates the logger for the specified name.
    /// </summary>
    /// <param name="loggerName">Name of the logger.</param>
    /// <returns></returns>
    public static Logger CreateLogger(string loggerName)
    {
        return new Logger(loggerName);
    }
}

现在这些类型将能够与他们自己的类型传递到创建方法实例审计。

Now those types will be able to instantiate your Auditor with their own type being passed to the Create method.

更新 -

我使用了统一容器注入依赖这个code为好。相关位是InjectionFactory。它让我在本地指定的静态方法注入基于某些条件类型。

I used the Unity container to inject dependencies into this code as well. The relevant bits is the InjectionFactory. It allows me to specify a static method locally to inject a type based on some conditions.

DependencyInjection.Container.RegisterType<IFormatter>(
    new ContainerControlledLifetimeManager(),
    new InjectionFactory(c => CreateFormatter(filename, outputType)));

...

        private static IFormatter CreateFormatter(string filename, OutputType outputType)
        {
            TextWriter textWriter = (filename.Length > 0) ? new StreamWriter(filename) : new StreamWriter(Console.OpenStandardOutput());
            var formatter = (outputType == OutputType.Xml) ? (IFormatter)new XmlFormatter(textWriter) : new CsvFormatter(textWriter);

            return formatter;
        }

...
// Calling it
using (var formatter = DependencyInjection.Container.Resolve<IFormatter>())
{
    if (timeLimit <= 1000) retval = OutputResults(results, formatter, msxQuery);
    else TimedOutputResults(results, formatter, msxQuery, timeLimit, OutputResults);
}

好了,做它至少对我来说!

Well, that does it for me at least!

这篇关于设置log4net的记录器的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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