Skip to content

Dynamic log name using log4net.Util.PatternString giving null reference exception after update to 3.0.0 (duplicate of #183) #186

@Noahbambi1

Description

@Noahbambi1

Hi,

After updating to Log4net 3.0.0, i believe there is a bug in the config when using log4net.Util.PatternString to accept dynamic values for file names.

I have attached a zipped dotnet project that demonstrates the error.
Log4NetExample.zip

If the project is run as is, the null reference error will occur.
The line causing this error is in the log4net.config, where the logfile is specified as so:
<file type="log4net.Util.PatternString" value="Logs/%property{LogName}" />

In addition some code snippets below with context from the attached project.

When configuring a file name for storing logs here is the simple config:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
	<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
		<file type="log4net.Util.PatternString" value="Logs/%property{LogName}" />
		<appendToFile value="true" />
		<rollingStyle value="Size" />
		<maxSizeRollBackups value="10" />
		<maximumFileSize value="10MB" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
		</layout>
	</appender>

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

In this case the "Logname" is set in the program.cs of the project.

class Program
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Program));

    static void Main(string[] args)
    {
        // Load the log4net configuration
        var logRepository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly());
        XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

        log4net.GlobalContext.Properties["LogName"] = "custom_log_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
        // Test logging
        log.Info("Application is starting.");
        log.Debug("This is a debug message.");
        log.Error("This is an error message.");
        log.Warn("This is a warning message.");
        log.Fatal("This is a fatal error message.");

        Console.WriteLine("Check the log file in the Logs folder.");
    }
}

With this setup, a null reference error occurs:

log4net:WARN Unable to set property [file] on object [log4net.Appender.RollingFileAppender] using value [Logs/%property{LogName}] (with acceptable conversion types)
log4net:ERROR Could not create Appender [RollingFileAppender] of type [log4net.Appender.RollingFileAppender]. Reported error follows.
System.NullReferenceException: Object reference not set to an instance of an object.
   at log4net.Appender.RollingFileAppender.ActivateOptions()
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [RollingFileAppender] not found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions