什么是在Autofac手动注册log4net的记录器实例ILog的缺点? [英] What is the disadvantage of manually registering Log4Net Logger instance to ILog in Autofac?

查看:1307
本文介绍了什么是在Autofac手动注册log4net的记录器实例ILog的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Autofac有 log4net的集成模块名为 LoggingModule

不过,我注册的日志的ILog 手动无需使用 LoggingModule ,似乎工作的罚款。

  ILog的日志= LogManager.GetLogger(记录仪);
builder.RegisterInstance(日志)。至于与下; ILog的>()SingleInstance();

我的问题是, - 什么是使用上述方法,而不是使用的 LoggingModule 的缺点/副作用。

的web.config

 <?XML版本=1.0编码=UTF-8&GT?;
<结构>
  < configSections>
    <节名称=log4net的TYPE =log4net.Config.Log4NetConfigurationSectionHandler,log4net的/>
  < / configSections>
  <&log4net的GT;
    <追加程序名称=RollingFileAppender进行TYPE =log4net.Appender.RollingFileAppender>
      <文件值=mylogfile.txt/>
      < appendToFile值=真/>
      < rollingStyle值=大小/>
      < maxSizeRollBackups值=5/>
      < maximumFileSize值=10MB/>
      < staticLogFileName值=真/>
      <布局类型=log4net.Layout.PatternLayout>
        < conversionPattern值=%DATE [%线程]%的水平记录仪% - %讯息%换行/>
      < /布局>
    < /附加器>
    <根和GT;
      <电平值=DEBUG/>
      <附加目的地-REF REF =RollingFileAppender进行/>
    < /根>
  < / log4net的>
< /结构>

的Global.asax.cs

  [汇编:log4net.Config.XmlConfigurator(手表= TRUE)]命名空间DemoLog4NetAuftofac
{
    公共类MvcApplication:一个HttpApplication
    {
        保护无效的Application_Start()
        {
            VAR建设者=新ContainerBuilder();            builder.RegisterControllers(typeof运算(MvcApplication).Assembly);            ILog的日志= LogManager.GetLogger(记录仪);
            builder.RegisterInstance(日志)。至于与下; ILog的>()SingleInstance();            变种容器= builder.Build();
            DependencyResolver.SetResolver(新AutofacDependencyResolver(容器));            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

的HomeController

 命名空间DemoLog4NetAuftofac.Controllers
{
    公共类HomeController的:控制器
    {
        公众的HomeController(ILog的日志)
        {
            log.Debug(调试应用程序);
            log.Error(应用程序错误);
            log.Info(信息应用);
        }
    }
}

版本

 <包ID =Autofac版本=3.4.0targetFramework =net45/>
  <包ID =Autofac.Mvc5版本=3.3.4targetFramework =net45/>
  <包ID =log4net的版本=2.0.3targetFramework =net45/>


解决方案

LoggingModule Autofac 的是一个自定义模块的先进的样品提供。

这个模块和定制登记的唯一区别在于,该模块将使用不同的的ILog 实例,其中被注入而在code各自类型您只能使用的ILog 的一个实例。

例如:

 公共类Foo
{
    公共美孚(ILog的日志)
    {
        Console.WriteLine(log.Logger.Name);
    }
}
公共类酒吧
{
    公共酒吧(ILog的日志)
    {
        Console.WriteLine(log.Logger.Name);
    }
}

使用自定义的注册登录注入酒吧将是相同的,他们将 LogManager.GetLogger(记录器)的结果。如果您使用 LoggingModule 日志的结果LogManager.GetLogger(typeof运算(富)) LogManager.GetLogger(typeof运算(酒吧))酒吧

的ILog 的不同实例,将帮助你针对特定类型之类的东西过滤日志。

您注册没有任何问题,但使用 LoggingModule 将允许您筛选或指定日志某个类或命名空间的水平。

顺便说一句,我建议你读 LoggingModule 源$ C ​​$ C,它是不是真的很难,它会帮助你更好地理解的模块的和的参数的作品有 Autofac

Autofac has log4net Integration Module called LoggingModule.

However, I register Logger to ILog manually without using LoggingModule, and it seems working fine.

ILog log = LogManager.GetLogger("Logger");
builder.RegisterInstance(log).As<ILog>().SingleInstance();

My question is - what is the disadvantage/side effect of using above approach instead of using LoggingModule.

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="mylogfile.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

Global.asax.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace DemoLog4NetAuftofac
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            ILog log = LogManager.GetLogger("Logger");
            builder.RegisterInstance(log).As<ILog>().SingleInstance();

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

HomeController

namespace DemoLog4NetAuftofac.Controllers
{
    public class HomeController : Controller
    {
        public HomeController(ILog log)
        {
            log.Debug("Debug application");
            log.Error("Error application");
            log.Info("Info application");
        }
    }
}

Version

  <package id="Autofac" version="3.4.0" targetFramework="net45" />
  <package id="Autofac.Mvc5" version="3.3.4" targetFramework="net45" />
  <package id="log4net" version="2.0.3" targetFramework="net45" />

解决方案

The LoggingModule provided by Autofac is an advanced sample of a custom Module.

The only difference between this module and a custom registration is that the module will use different ILog instance for each type where it is injected whereas in your code, you will only use one instance of ILog.

For example :

public class Foo
{
    public Foo(ILog log)
    { 
        Console.WriteLine(log.Logger.Name);
    }
}
public class Bar
{
    public Bar(ILog log)
    { 
        Console.WriteLine(log.Logger.Name);
    }
}

With your custom registration log injected to Foo and Bar will be the same and they will be the result of LogManager.GetLogger("Logger"). If you use the LoggingModule log will be the result of LogManager.GetLogger(typeof(Foo)) for Foo and LogManager.GetLogger(typeof(Bar)) for Bar.

Having different instance of ILog will help you filtering log for a specific type and things like that.

Your registration has no problem but using the LoggingModule will allow you to filter or specify log level for some class or namespace.

By the way, I recommend you to read the LoggingModule source code, it is not really difficult and it will help you better understand how Module and Parameter works with Autofac.

这篇关于什么是在Autofac手动注册log4net的记录器实例ILog的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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