1

我的代码是:

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>

记录器安装程序.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)。

4

1 回答 1

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在需要时将其设置为依赖项

于 2013-08-12T13:55:03.890 回答