Domanda

I am experiencing a strange issue with newest version of log4net 1.2.12 My Log file is created successfully without issue, anyway also a (null) file is always created in the same directory of the Exe file.

This is my xml config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
      <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="DEBUG"/>
        <acceptOnMatch value="false"/>
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <threshold value="ALL"/>
      <File type="log4net.Util.PatternString" value="%property{LogFileName}"/>
      <param name="appendToFile" value="false"/>
      <param name="maximumFileSize" value="20000KB"/>
      <param name="maxSizeRollBackups" value="200"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFile"/>
    </root>
  </log4net>
</configuration>

and this is how I call it from my main:

class Program
{
    /// <summary>
    /// The logger
    /// </summary>
    private static ILog logger = LogManager.GetLogger(typeof(Program));

    /// <summary>
    /// The CMD line args
    /// </summary>
    public static Arguments CmdLineArgs = null;

    /// <summary>
    /// Main.
    /// </summary>
    /// <param name="args">The arguments.</param>
    /// <returns></returns>
    static int Main(string[] args)
    {
        try
        {
            CmdLineArgs = new Arguments();

            Parser CmdLineParser = new Parser();

            if (CmdLineParser.ParseArguments(args, CmdLineArgs))
            {
                log4net.GlobalContext.Properties["LogFileName"] = CmdLineArgs.LogFile;
                log4net.Config.XmlConfigurator.Configure();

                CmdLineArgs.PrintArguments(args);
                CmdLineArgs.Check();
                CmdLineArgs.PrintArguments();
            }
            else
            {
                CmdLineArgs.PrintArguments(args);
                Console.WriteLine(CmdLineArgs.GetUsage());
                Console.WriteLine(string.Format("Return Code: {0}", 2));

                return 2;
            }

            logger.Info("Execution Terminated");
            logger.Info(string.Format("Return Code: {0}", 0));

            return 0;
        }
        catch (Exception ex)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Fatal("Fatal Error:", ex);
            }
            else
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(string.Format("Return Code: {0}", 8));
            }

            logger.Info(string.Format("Return Code: {0}", 8));

            return 8;
        }
    }
}

This perfectly works with log4net 1.2.11 and previous, but with log4net 1.2.12 a (null) file is created.

I am using VS2012 and .net 4.0 Framework.

È stato utile?

Soluzione

You probably have a line like

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

in your AssemblyInfo.cs.

That will initialise log4net and create your (null) logfile when the first log4net dll access takes place, which in your example is when it executes the line

private static ILog logger = LogManager.GetLogger(typeof(Program));

This will happen before the Main method is called and before your Logfile property is set.

Altri suggerimenti

I suspect this is because you get an instance of ILog in a static field: that static field will be created before the configuration code has run and the log file name has been set, hence creating a null file as LogFileName is null.

Then, when you set the log file name and call Configure() the second file is created.

Try change

<File type="log4net.Util.PatternString" value="%property{LogFileName}"/>

to become

<param name="file" type="log4net.Util.PatternString" value="%property{LogFileName}"/>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top