Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net %property does not work

I have a small test project with the following code:

class Program
{
    static void Main(string[] args)
    {
        log4net.GlobalContext.Properties["logFileName"] = "log.txt";
        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.xml"));

        log4net.ILog logger = log4net.LogManager.GetLogger("Tests");

        logger.Debug("Test message");
    }
}

My log4net.xml config file has the following content:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="%property{logFileName}" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%utcdate{ISO8601} [%level][%logger] %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

My question is why does log4net create file with name %property{logFileName} and doesn't substitutes it with log.txt?

log4net library is taken from NuGet (Id:log4net Version:2.0.3).

like image 597
Pavels Ahmadulins Avatar asked May 14 '15 13:05

Pavels Ahmadulins


People also ask

How do I use multiple Appenders in log4net?

You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.

What is rolling file Appender in log4net?

RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.


1 Answers

Please use this in your xml config

<file type="log4net.Util.PatternString" value=".\%property{logFileName}" />
like image 197
michalh Avatar answered Oct 09 '22 09:10

michalh