4

My log4net is creating an empty log file in my debug/release folders for some reason. An example filename is "(null) 20150409.txt".

I'm not sure how to tell it not to do that. I am programmatically configuring log4net, but this empty file is created before the configuration routine is actually run, perhaps as a result of the App.config file itself.

Here is the App.config file:

<?xml version="1.0"?>
    <configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>

  <log4net>
    <root>
      <level value="ALL"/>
      <appender-ref ref="LogFileAppender"/>
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
      <!--http://logging.apache.org/log4net/release/config-examples.html-->
      <!--http://logging.apache.org/log4net/release/faq.html-->

      <!--<param name="File" value="C:\Images\log-file.txt" />-->
      <file type="log4net.Util.PatternString" value="%property{LogFileName}"/>

      <staticLogFileName value="false"/>
      <param name="AppendToFile" value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="' 'yyyyMMdd'.txt'"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>
  </log4net>

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>

</configuration>

Here is the C# configuration code (which appears to work fine):

log4net.GlobalContext.Properties["LogFileName"] = LogFileName;  
// Full path to log file
log4net.Config.XmlConfigurator.Configure();
Log.Debug("CV3 Launched with log file located here: " + LogFileName);

How do I prevent this empty file from being created without affecting the rest of the logging which is working fine?

stuartd
  • 70,509
  • 14
  • 132
  • 163
nb1forxp
  • 385
  • 2
  • 14
  • Did you try to use ignore section handler in `app.config`? That should avoid the empty file. – Lex Li Apr 10 '15 at 03:09
  • Where/how in app.config do I put the ignore section handler? Can you please provide a snippet? Thanks. – nb1forxp Apr 10 '15 at 05:08
  • 3
    Do you have `[assembly: log4net.Config.XmlConfigurator(Watch = true)]` in your AssemblyInfo.cs file? – sgmoore Apr 10 '15 at 08:24
  • I believe is because of the `datePattern` element. Also, you're using a RollingFileAppender, but when it rolls it will not use the log file name you've supplied, but will create one based on the datePattern.. – stuartd Apr 10 '15 at 15:32
  • http://logging.apache.org/log4net/release/sdk/log4net.Config.XmlConfigurator.Configure_overload_5.html It shows how to. – Lex Li Apr 11 '15 at 00:32

2 Answers2

2

I have the same problem. Doing research around (here, here and here), the error is because that you have called log4net.Config.XmlConfigurator.Configure(); before setting log4net properties.

If you are like me, very likely that you have configured log4net another time in the file AssemblyInfo.cs. Because the logger in AssemblyInfo would be loaded first, it would create another (null) file every time your app start.

Hope it helps.

Community
  • 1
  • 1
Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
0

@nb1forxp , have you tried this way?

Create a new locking model

public class MyLock : log4net.Appender.FileAppender.MinimalLock
{
      public override Stream AcquireLock()
      {
            if (CurrentAppender.Threshold == log4net.Core.Level.Off)
                  return null;

            return base.AcquireLock();
      }
}

Config

<threshold value="OFF" />
<lockingModel type="Namespace.MyLock" />

Refers from How to disable creation of empty log file on app start?.

Community
  • 1
  • 1
Angus Chung
  • 1,547
  • 1
  • 11
  • 13