Log4Net is creating file but not writing to it

audit-logging c# entity-framework-6 log4net log4net-configuration


Recently i had an issue making Log4Net work (described here) but after that it was OK.

I have left this behind a while because i needed to develop some modules and i left the logging somewhat behind. Now that i look, i have even tried changing the name of the log file and the location (set it statically), it is creating it but not writing anything to it in both cases.

This is my log4Net config file:

<?xml version="1.0"?>

 <level value="ALL" />
 <appender-ref ref="console" />
 <appender-ref ref="file" />
<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
<appender name="file" type="log4net.Appender.RollingFileAppender">
 <file value="ApplicationLogging.log" />
 <appendToFile value="true" />
 <rollingStyle value="Size" />
 <maxSizeRollBackups value="5" />
 <maximumFileSize value="10MB" />
 <staticLogFileName value="true" />
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%date [%thread] %level %logger - %message%newline" />

This is my Global.asax

[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

 ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 logger.Info("Application started.");

How i declare it:

 readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

I than use it sometimes like this:

logger.Info("some logging here");

Or for Context logging i would use it like this:

context.Database.Log = (dbLog => logger.Debug(dbLog));

The file is created, but no content is written to it. Can anyone suggest me where or what to look for?

UPDATE: As suggested by stuartd i have added this in the web.config:

 <add key="log4net.Internal.Debug" value="true"/>

 <trace autoflush="true">
        initializeData="C:\log4net.txt" />

Which writes the following content (pastebin)

Note that i have removed the first section which i didn't saw before and i think it is redundant?

<!--<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />

Either way with or without it it doesn't work.

Here is the output WITH the first part NOT commented out (in its original state)

I tried the following as well: https://logging.apache.org/log4net/release/config-examples.html https://csharp.today/log4net-tutorial-great-library-for-logging/

I am clueless on why it can create it but not write to it... I can not seem to find anything on the web neither, all issues are regarding the creation of the file, not writing to it.

12/4/2018 3:41:45 PM

Accepted Answer

+1 for including the debug/trace log in your question.
The message log4net: Configuring Repository [log4net-default-repository] appears multiple times in this log, which looks like the configuration is being set up twice, once via the XmlConfiguratorAttribute and a second time via the call to XmlConfigurator.Configure();.

Inspecting the source code of the XmlConfiguratorAttribute reveals that it internally makes a similar call to XmlConfigurator.Configure(...);

private void ConfigureFromFile(ILoggerRepository targetRepository, FileInfo configFile)
    if (m_configureAndWatch)
        XmlConfigurator.ConfigureAndWatch(targetRepository, configFile);
        XmlConfigurator.Configure(targetRepository, configFile);

This is what happens.

The XmlConfiguratorAttribute initially sets up the loggers and appenders as defined in the log4net.config file, and creates the empty ApplicationLogging.log file.

Then the call to XmlConfigurator.Configure(); overwrites these loggers and appenders with the configuration as in web.config (being the default location) which doesn't contain any as you are using a separate log4net.config file.
Because of this, you end up without any Appender, and nothing gets logged.

The solution is to apply XmlConfiguratorAttribute or call XmlConfigurator.Configure() with the path to the log4net.configfile (XmlConfigurator.Configure(new FileInfo("log4net.config"))), but don't do both.

12/5/2018 10:01:41 AM

Popular Answer

Please check if your application has write permissions on destination folder for log file, Those kind of things can sometimes lead to a real pain.

Besides everything else looks quite good.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow