5

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"?>
<configuration>    

<log4net>
<root>
 <level value="ALL" />
 <appender-ref ref="console" />
 <appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
  </layout>
</appender>
<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" />
 </layout>
</appender>
</log4net>
</configuration>

This is my Global.asax

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

 XmlConfigurator.Configure();            
 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:

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

<system.diagnostics>
 <trace autoflush="true">
  <listeners>
    <add
        name="textWriterTraceListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="C:\log4net.txt" />
  </listeners>
 </trace>
</system.diagnostics>

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" />
  </layout>
</appender>-->

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.

Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • Is it logging to the console? – sellotape Dec 04 '18 at 14:28
  • No it isn't since i don't have any console, it is a MVC web application. – Dimitri Dec 04 '18 at 14:30
  • 2
    So why have you got a console appender? And why do you have a `` section in your log4net config file? Anyway I suggest you [turn on log4net internal logging](https://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems) and see what it reports. – stuartd Dec 04 '18 at 14:30
  • 1
    I don't know if this has to do anything with your problem, but you're setting the RollingFile properties to size meaning that you want to have new files as the older ones get heavier. [This](http://logging.apache.org/log4net/log4net-1.2.12/release/sdk/log4net.Appender.RollingFileAppender.StaticLogFileName.html) line `` is saying that only one file is going to be used for logging. That might give you a bit of a headache later even if it's not causing this problem. – ikerbera Dec 04 '18 at 14:41
  • Thank you for pointing that out @ikerbera. – Dimitri Dec 04 '18 at 14:52
  • 1
    Weird to see `configuring repository [log4net-default-repository]` multiple times in the debug/trace log; as if you are configuring twice. Can you remove `XmlConfigurator.Configure();`. Also add `` to ensure the internal buffers get flushed immediately. – pfx Dec 04 '18 at 17:46
  • @pfx, Removing the XmlConfigurator.Configure(); did the trick ! I love you (in a decent way). Maybe you can elaborate more on how this is possible, what is configuring it, i it is done twice? This resolved my issue. THANK YOU SO MUCH! – Dimitri Dec 05 '18 at 08:13

2 Answers2

3

+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);
    }
    else
    {
        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.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • Thank you so much for the the well explained solution! This totally makes sense but i wouldn't have thought of that to be honest. Thank you **stuartd** as well because of you i was able to introduce the 'debug/trace log' which eventually has brought the answer! I love stackoverflow and its user :) – Dimitri Dec 05 '18 at 10:10
1

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.

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 4
    This isn't an answer and should be a comment. – stuartd Dec 04 '18 at 14:30
  • @Barr J, i am sure that my app has the rights on the destination folder, to be sure i have set the path to the same directory as the 'log4net.Internal.Debug' which is creating and writing correctly (as suggested by **stuartd**). Changing paths, names or extensions didn't help neither. Updating the original post with the added configuration settings in the web.config. – Dimitri Dec 04 '18 at 14:55
  • Original post updated. I tried removing the ref to the console appender as well as the section, leaving only the file section and ref but i still don't have any content to my file... – Dimitri Dec 04 '18 at 15:19
  • @BarrJ, didn't work neither. When debugging, i looked into the Appender object itself and this is what i have: https://imgur.com/a/S5ukpeS . Shouldn't the Count be > 0 ? – Dimitri Dec 05 '18 at 07:45