log4net:ERROR使用null'element'参数调用ConfigureFromXml

时间:2018-02-27 04:06:56

标签: asp.net-core log4net

我们正在使用带有.NET Core的log4net库来编写日志文件。但是我们在执行应用程序“log4net:ERROR ConfigureFromXml with null'lelement'parameter'时遇到此错误。 这是我们的Program.cs文件:

public class Program
{
    public static void Main(string[] args)
    {
        XmlDocument log4netConfig = new XmlDocument();
        log4netConfig.Load(File.OpenRead("log4net.config"));
        var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
        log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

这是Startup.cs文件中的Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        loggerFactory.AddConsole(LogLevel.Trace);
        loggerFactory.AddLog4Net();

        app.UseIdentityServer();
    }

这是log4net.config文件:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
  </configSections>

  <!-- Log4net Logging Setup -->
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
      <file value="c:\LogFiles\Applications\IdentityServer\log.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>

    <root>
      <level value="DEBUG"/>
      <appender-ref ref="FileAppender"/>
    </root>
  </log4net>
</configuration>

1 个答案:

答案 0 :(得分:1)

实际上,接受的响应不准确,XML的问题是在log4net配置之上的.NET配置。原始配置文件是.NET配置文件,而不是log4net文件,它的根目录是 configuration 节点,而不是 log4net 节点。

另一方面,第二个配置XML是正确的,但是它正在替换原始配置(可以肯定是一个更好的配置),不仅解决了问题,而且用新的替换了原始配置。

对此更改的更好响应是:

  1. 从原始xml文件中删除 configuration 根节点以及 configSections 节点,并将log4net节点保留为XML的根目录,从而导致:

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- Log4net Logging Setup -->
    <log4net>
      <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
        <file value="c:\LogFiles\Applications\IdentityServer\log.txt" />
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="INFO" />
          <levelMax value="FATAL" />
        </filter>
      </appender>
    
      <root>
        <level value="DEBUG"/>
        <appender-ref ref="FileAppender"/>
      </root>
    </log4net>
    
  2. 根据需要使用滚动附加程序重新配置附加程序...