没有日志记录,并且存储库根目录没有附加程序,但Logger没有附加程序

时间:2018-10-17 22:53:29

标签: c# logging log4net xunit

我确定我缺少一些基本知识。我正在尝试在XUnit集成测试中启用log4net日志记录,但是没有日志文件写入磁盘。

我尝试同时使用配置文件和以编程方式指定日志配置:

配置文件:

 <log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file value="mylog.log" />
    <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>
</log4net>

以编程方式

    private void ConfigureAppender()
    {
        var layout = new PatternLayout("%date [%thread] %level %logger - %message%newline");
        var appender = new RollingFileAppender {Name = "file", File = "c:\\temp\\mylog.log", AppendToFile = true, RollingStyle = RollingFileAppender.RollingMode.Size, MaxSizeRollBackups = 5, MaximumFileSize = "10MB", StaticLogFileName = true, Layout = layout};
        var repository = LogManager.GetRepository() as Hierarchy;
        repository.Root.AddAppender(appender);
        repository.Root.Level = Level.Info;
        repository.Configured = true;
        repository.RaiseConfigurationChanged(EventArgs.Empty);
    }

然后我使用这种方法(我也尝试过[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)])在程序执行的尽可能早的时候得到记录器:

        var fileInfo = new FileInfo("log4net.config");
        if (!fileInfo.Exists) throw new Exception();
        XmlConfigurator.Configure(fileInfo);
        var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        log.Fatal("This is a test");

两种方式均无日志。无论是通过config还是以编程方式定义日志记录,当我检查日志对象时,我都会在底部看到屏幕截图。请注意以下几点:

  1. 级别设置已从配置定义中提取。
  2. 存储库根目录具有配置定义中定义的两个追加程序。
  3. Logger实例本身没有附加程序。

有人可以告诉我我想念什么吗?

enter image description here

编辑: 根据Z.R.T.的答案,这就是我想念的:

  • 我在log4net.config / app.config中需要一个logger元素。
  • 我需要按名称引用记录器,例如LogManager.GetLogger("IntegrationLogger");

一旦我添加了这两件事,log4net就开始工作。唯一的缺点是,所有日志都标记有“ IntegrationLogger”,而不是生成日志的类,但这至少开始了日志记录。

1 个答案:

答案 0 :(得分:2)

log4net.config -设置副本(如果较新或始终复制)

<log4net>
  <appender name="SomeRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="..\\logs\\loginfo.log" />
    <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>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
  <logger name="SomeLogger">
    <level value="INFO" />
    <appender-ref ref="SomeRollingLogFileAppender" />
    <appender-ref ref="console" />
  </logger>
</log4net>

Log.cs

public class Log
{
    private static readonly ILog someLogger = LogManager.GetLogger("SomeLogger");
    static Log()
    {
        if (!UnitTestDetector.IsRunningFromNUnit)
        {
            XmlConfigurator.Configure(new FileInfo("log4net.config"));
        }
        else
        {
            var testDir = TestContext.CurrentContext.TestDirectory;
            XmlConfigurator.Configure(new FileInfo(Path.Combine(testDir,"log4net.config")));
        }
    }

    public static void Info(string msg)
    {
        someLogger.Info(msg);
    }
}

static class UnitTestDetector
{
    static UnitTestDetector()
    {
        foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
        {
            if (assem.FullName.ToLowerInvariant().StartsWith("nunit.framework"))
            {
                IsRunningFromNUnit = true;
                break;
            }
        }
    }

    public static bool IsRunningFromNUnit { get; } = false;
}

Program.cs

[TestFixture]
class Program
{

    static void Main()
    {
        Log.Info("test");
        Console.ReadLine();
    }

    [Test]
    public void Test()
    {
        Log.Info("test");
    }
}