如果使用日志记录外观,在使用IoC/DI时是否应该注入日志记录基础结构? [英] Should logging infrastructure be injected when using IoC/DI if logging facade is used?

查看:64
本文介绍了如果使用日志记录外观,在使用IoC/DI时是否应该注入日志记录基础结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Autofac作为我的IoC,并且我在DI主题上阅读的所有内容都教导使用构造函数注入"来显式公开类依赖关系... 但是,我还在Log4Net中使用日志记录外观(Common.Logging),并创建了Autofac模块来注入它. 现在,在每个我想进行日志记录的类中,我都有额外的构造函数参数(请参见示例1)....

I am using Autofac as my IoC and from everything I have read on topic of DI teaches to use "constructor injection" to explicitly expose class dependencies... However, I am also using logging facade (Common.Logging) with Log4Net and have created Autofac module to inject it. Now, in every class in which I want to do some logging I have extra constructor parameter (see sample #1)....

我想知道使用记录外观时是否需要记录DI? 我知道通过构造函数签名显式公开依赖项是一个好的体系结构. 但是在记录立面的情况下,我相信以下是正确的:

I am wondering if logging DI is necessary when using logging facade? I understand that explicitly exposing dependencies via constructor signature is a good architecture. but in case of logging facade I believe following is true:

  • 我仍然可以随时交换"日志记录框架
  • 恕我直言,类并非真正依赖Logger.如果未配置日志记录,则使用NullLogger.这几乎是如果需要它的话"与除非您提供它就无法工作"这样的交易……(请参见示例2)

那么,其他人怎么看?注入伐木立面是不是太过分了? 关于此主题,有一些类似的问题,但笼统地说是(

So, what do others think? Is injecting logging facade an overkill? There are some similar questions on this topic but in more general terms (infrastructure) - I am mainly interested in logging....

// IoC "way"
public class MyController : BaseController
{
    private readonly ILog _logger;

    public MyController(ILog logger)
    {
        _logger = logger;
    }

    public IList<Customers> Get()
    {
        _logger.Debug("I am injected via constructor using some IoC!");
    }
}

// just use the logger "way"
public class MyController : BaseController
{
    private static readonly ILog Logger = LogManager.GetCurrentClassLogger();

    public IList<Customers> Get()
    {
        Logger.Debug("Done! I can use it!");
    }
}

推荐答案

日志记录只是基础架构.注射它是多余的.我个人甚至不使用抽象层.我使用库直接提供的静态类.我的动机是,我不太可能在当前项目中切换日志记录库(但可能会切换到下一个项目).

Logging is just infrastructure. Injecting it is overkill. I personally don't even use an abstraction layer. I use the static classes that the library provides directly. My motivation is that it's unlikely that I'll switch logging library in a current project (but might switch for the next project).

但是,您在示例中使用的是控制器.为什么需要登录他们?控制器只是视图和模型(业务逻辑)之间的适配器.无需登录.

However, you are using controllers in your example. Why do you need to log in them? A controller is just an adapter between the view and the model (business logic). There should be no need to log in it.

通常,您仅登录包含业务逻辑的类,并在顶层登录才能记录未处理的异常.这些是很难调试的情况,因此是需要记录日志的地方.

You typically do only log in classes which contains business logic and at the top layer to be able to log unhandled exceptions. Those are the hard cases to debug and therefore the places where logging is required.

必须在其他地方登录表明您需要重构以正确封装业务逻辑.

Having to log at other places indicates that you need to refactor to encapsulate your business logic properly.

这篇关于如果使用日志记录外观,在使用IoC/DI时是否应该注入日志记录基础结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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