Declare a new log4net logger using Ninject

放肆的年华 提交于 2019-12-12 01:56:53

问题


I'm developing a Web Api application with C#, .NET Framework 4.0, Ninject 3.2.2.0 and log4net 2.0.3.

Now, I only using one logger. This is how I configure if on NinjectConfigurator class.

private void ConfigureLog4net(IKernel container)
{
    log4net.Config.XmlConfigurator.Configure();
    var loggerForWebSite = LogManager.GetLogger("AutomationMiddlewareWebsite");
    container.Bind<ILog>().ToConstant(loggerForWebSite);
}

But, I need to use another logger. Using, Ninject, how can I do it? I'm don't know how to declare this new logger in NinjectConfigurator class.

If I declare another logger, I can't use ILog to bind it to my new logger. Maybe, I can use bind it with parameters...

This is my log4net configuration in Web.config.

<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="d:\\MyProject.Web.Api.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value=".yyyyMMdd.lo\g" />
      <maximumFileSize value="5MB" />
      <maxSizeRollBackups value="-1" />
      <countDirection value="1" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level [%thread] %logger - %message%newline%exception" />
      </layout>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %-5level [%thread] %logger - %message%newline%exception" />
      </layout>
    </appender>
    <logger name="MyProjectWebsite">
      <level value="DEBUG" />
    </logger>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

回答1:


You could bind multiple loggers .ToConstant(...) and use Contextual Binding facilities like .WhenInjectedInto<SomeClass>().

Bind<Ilog>().ToConstant(logger1)
    .WhenInjectedInto<Class1>();

But if you need to inject the logger into a lot of classes that would present a lot of work and very poor maintainability.

In that case you can opt to use the ToMethod(..) binding or use a provider and create the logger depending on the context, like so:

Bind<ILog>().ToMethod(context =>
                 LogManager.GetLogger(context.Request.ParentContext.Plan.Type));

(context.Request.ParentContext.Plan.Type is the Type ILog is injected into)

of course you can use some elaborate scheme to determine which logger to use based on the class name, the class assembly, the class namespace,.. you name it.

If you want only one or two specific loggers you can use the contextual binding (When(...)) approach and if more, you can use the ToMethod(...) or IProvider approach.



来源:https://stackoverflow.com/questions/27444920/declare-a-new-log4net-logger-using-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!