在log4net包装器中使用structuremap [英] Using structuremap with log4net wrapper

查看:116
本文介绍了在log4net包装器中使用structuremap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下界面:

public interface ILogger
{
    void Debug(string message, params object[] values);
    void Info(string message, params object[] values);
    void Warn(string message, params object[] values);
    void Error(string message, params object[] values);
    void Fatal(string message, params object[] values);
}

和以下实现:

public class Log4netLogger : ILogger
{
    private ILog _log;

    public Log4netLogger(Type type)
    {
        _log = LogManager.GetLogger(type);
    }

    public void Debug(string message, params object[] values)
    {
        _log.DebugFormat(message, values);
    }

    // other logging methods here...

}

我的想法是使用Structuremap实例化Log4netLogger类,并使用执行日志记录的类的Type.但是,我一辈子都无法弄清楚如何将调用类的类型传递给structuremap,以便可以将其传递给日志记录实现的构造函数.任何关于如何做到这一点(或更好的方法)的建议,将不胜感激.

My idea was to use structuremap to instantiate the Log4netLogger class with using the Type of the class that did the logging. However, I can't for the life of me figure out how to pass the type of the calling class to structuremap so that it can be passed to the constructor of the logging implementation. Any advice on how to do that (or a better way) would be most appreciated.

推荐答案

如果type参数是特定于上下文的,那么我认为这将无法正常工作.如果需要在构造函数中传递特定于上下文的内容,则可能必须创建一个工厂接口和实现,以返回ILogger的实例:

If the type parameter is context-specific, I don't think this is going to work as shown. If you need to pass something context specific in the constructor, you are likely going to have to create a factory interface and implementation that returns an instance of the ILogger:

public interface ILoggerFactory
{
    ILogger Create(Type type);   
}

public class LoggerFactory : ILoggerFactory
{
    public ILogger Create(Type type)
    {
        return new Log4netLogger(type);
    }
}

也许可以引导StructureMap来根据类型提供所需的实例,但是假定您事先知道的类型数量有限.

It might be possible to bootstrap StructureMap to supply the instance you want based on the type, but that assumes a limited number of types that you know in advance.

这篇关于在log4net包装器中使用structuremap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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