使用Castle Windsor,Logging Facility和log4net进行记录

时间:2013-08-12 12:08:07

标签: log4net castle-windsor

我的代码是:

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; }
}

当我使用Log.Error("some exception")

在日志文件中,我有来自其他dll的其他日志。我只希望我的日志,而不是其他dll异常,只显式来自调用(Log.Error)。

1 个答案:

答案 0 :(得分:3)

你必须使用

<logger name="MyLog"> 

而不是

<root> 

在log4net.config

您也可以在windsor regitration中使用ToLog方法

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

不确定你的MyLogger.cs:由于windsor设施,所以不应该...... 只需在需要时将ILogger设置为依赖