Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net:ERROR ConfigureFromXml called with null 'element' parameter

We are using log4net library with .NET Core for writing logs in file. But we are getting this error when executing the application "log4net:ERROR ConfigureFromXml called with null 'element' parameter". Here is our Program.cs file:

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

Here is Configure method in Startup.cs file:

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

        app.UseIdentityServer();
    }

Here is log4net.config file:

    <?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>
like image 618
Rakesh Kumar Avatar asked Oct 20 '25 13:10

Rakesh Kumar


2 Answers

Replace your log4net.config with below code :

    <?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <file value="C:\Temp\" />
    <datePattern value="yyyy-MM-dd.'txt'"/>
    <staticLogFileName value="false"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <maxSizeRollBackups value="100"/>
    <maximumFileSize value="15MB"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level App  %newline %message %newline %newline"/>
    </layout>
  </appender>
  <root>
    <level value="ALL"/>
    <appender-ref ref="RollingLogFileAppender"/>
  </root>
</log4net>
like image 149
Deepak Kumar Avatar answered Oct 22 '25 04:10

Deepak Kumar


In fact, the accepted response is not accurate, the problem with the XML is the .NET configuration on top of log4net configuration. The original configuration file is a .NET configuration file instead of a log4net one, and it's rooted with a configuration node instead of with a log4net node.

The second configuration XML on the other hand is correct, but it's replacing the original configuration (maybe with a better one, for sure), not only fixing the problem but replacing the original configuration with a new one.

The a better response for the change would be:

  1. Remove the configuration root node from the original xml file together with the configSections node and leave the log4net node as root of the XML resulting in:

    <?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. Reconfigure your appender with a Rolling appender if needed...

like image 34
Eugenio Miró Avatar answered Oct 22 '25 03:10

Eugenio Miró



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!