38

I am currently building an ASP.Net-MVC Application using log4net for logging, but the logger seems to just stop at random. It will happily log for awhile, and then stop, and then will start again after a period of time. I am not even sure what it is that makes it resume logging. I'm not talking about just a few messages being lost- sometimes it dissappears for a long period of time, such as an hour or so.

Why would it stop and start like this? How should I properly configure this so that it will not randomly stop as it does?

Here is my configuration:

<log4net debug="true">
<appender name="RollingLogFileAppender"
        type="log4net.Appender.RollingFileAppender">

  <file value="..\Logs\\CurrentLog.txt" />
  <appendToFile value="true" />
  <datePattern value="yyyyMMdd" />

  <rollingStyle value="Date" />
  <filter type="log4net.Filter.LevelRangeFilter">
    <acceptOnMatch value="true" />

    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>

  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern
    value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
  </layout>

</appender>

<root>
  <level value="INFO" />
  <appender-ref ref="RollingLogFileAppender" />
</root>

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Jacob Bellamy
  • 769
  • 2
  • 10
  • 16
  • 1
    In my case it was resetting the logging (re-configuring it, not by design..) that caused these sort of problems - see http://neilkilbride.blogspot.com.au/2008/04/configure-log4net-only-once.html – Jonno Aug 09 '12 at 03:10
  • Would you mind adding an answer or comment explaining what caused your problem, if you found a reason? – Mark Hurd May 20 '13 at 01:26
  • Yes, it would be nice to know what caused your problem as Mark said. By the way, I was having the same issue and setting the ReconnectOnError to true under the ADONetAppender config worked for me. – Jportelas Jul 15 '13 at 19:15
  • I've added XmlConfigurator.Configure in one of my modules for debugging purposes, and it failed because the app has already configured it's loggers from another config file. – surfen Apr 27 '14 at 16:49
  • 1
    For sure you can't trust something you don't understand. IMO Log4net is as complex as it must be, to accomplish what you can do with it. Complexity is often a reflection of flexibility. Internal logging is super useful, btw. – Alberto Chiesa Feb 09 '17 at 10:07

4 Answers4

64

Log4Net will fail silently if something goes wrong and it is unable to write to its appenders. This is actually a good thing, since it means a bit of failed logging won't bring down an otherwise healthy system, but it can be annoying when something isn't logging as you expect.

Your best bet is to turn on log4net's own internal logging to do some diagnostics and (hopefully) work out why it's failing.

So in your app's config file add:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

which will turn on the internal logging, which gets sent to System.Diagnostics.Trace, so you can add:

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

to capture this to a file.

gavriln
  • 19
  • 3
Alconja
  • 14,834
  • 3
  • 60
  • 61
  • 2
    Do you have a fuller example of such a config file? I've tried numerous variations, and I keep getting XML errors from the XmlSerializationReader. Even just an empty `` tag is the difference between exception and not. – Grant Birchmeier Mar 04 '14 at 23:29
  • I'm having the same problem, and in my case it looks to be that anytime I open a standard file dialog, the [Box](https://box.com) icon overlay DLL overwrites the in-process log4net.config with its own. – Chris R. Donnelly Feb 01 '16 at 19:25
  • 1
    This is actually NOT a good thing since there can be system monitoring and error notification tools depending on it and if it stops logging, there is no way we can know an exception occur. Hate this crappy logging tool, it's always a headache whichever company I go, whatever version used, there is always a headache. Crappy developers' crappy product.. – yakya May 12 '17 at 12:11
  • OK, but what do I do if _that_ log shows me nothing special ? Any links on how to investigate further ? (I'm not going to fight this wall for very long. If log4net doesn't work and doesn't tell me what's wrong, I'll end up ditching it and find out something that I _can_ debug) – Fred vdP Jan 26 '22 at 08:58
  • I had forgotten to add [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the assembly I was newly trying to log (part of a years-old multi-assembly project which already used log4net in other parts, apparently more or less working). After adding that,, my corrected program no longer produces anything in the "log4net.txt" file. Which means that the previous output I found in "log4net.txt" was indicative of the error ? That output gave me no clue as to whether anything wrong occurred (let alone what went wrong). – Fred vdP Jan 26 '22 at 09:17
2

Send out a test log message as early as you can in your application lifecycle, just a simple LogManager.GetLogger("Init").Info("Starting logging") will do. If some other referenced code gets first to starting logging, a part of your initialization process may fail (as it loads settings from another assembly, which it only does once).

Read here: https://logging.apache.org/log4net/release/faq.html#first-log

2

Also check if log4net is still configured in the AssemblyInfo.cs file. In my case a check-in caused the following line to be removed in this file:

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

Which in turn caused log4net to stop logging...

Sego
  • 310
  • 2
  • 5
0

After struggling with Log4Net for a while, I found out that it would stop logging altogether. Not matter what I tried I couldn't get it back to work. It seems that there are issues when you do not initialize the log in the first few lines of code but that fuzzy indication was not enough for me to reliably get the log to work within a dll loaded by another program and so on.

I never managed to activate the internal log either.

I finally resolved to create a dedicated dll that my programs can call to reliably create a log using Log4Net. The dll does only that and the code creating the log is isolated and never changes. I assume that this way I will be able to reliably create logs files and write to them. So far this approach has fixed the issues with logging on which I had spend countless frustrating hours.

I posted the code on this SO thread:

stackoverflow.com/questions/308436/log4net-programmatically-specify-multiple-loggers-with-multiple-file-appenders

pasx
  • 2,718
  • 1
  • 34
  • 26