Logging with Castle Windsor, the Logging Facility and log4net

烈酒焚心 提交于 2019-12-24 15:33:35

问题


My code is :

log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <appender name="MyLog" type="log4net.Appender.RollingFileAppender">
      <file value="logs\log-file.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <maxSizeRollBackups value="2" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %thread %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="MyWayLog" />
    </root>
  </log4net>
</configuration>

LoggerInstaler.cs

public class LoggerInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config"));
    }
}

MyLogger.cs

private static ILoggerFactory loggera = IoC.Container.Resolve<ILoggerFactory>();

private static ILogger logger = loggera.Create("MyLog"); 

public static ILogger Log
{
    get { return logger; }
    set { logger = value; }
}

When I Use Log.Error("some exception")

In log file I have other logs from some other dll. I want only my logs, not the other dll exception, only explicitly from call (Log.Error).


回答1:


You have to use

<logger name="MyLog"> 

instead of

<root> 

in the log4net.config

You may also use ToLog method within windsor regitration

container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config").ToLog("MyLog"));

Not sure about your MyLogger.cs: Due to windsor facility, there's should no need for that... simply set ILogger as dependency when you need it



来源:https://stackoverflow.com/questions/18186595/logging-with-castle-windsor-the-logging-facility-and-log4net

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