208

I use log4net all the time, but one thing I've never figured out is how to tell what's going on on the inside. For example, I've got a console appender and a database appender in my project. I made a few changes to the database and the code, and now the database appender doesn't work anymore. I'll figure out why eventually, but it would help a lot if I could see what's going on inside log4net.

Does log4net generate any kind of output that I can view to try to determine the source of my problem?

John M Gant
  • 18,970
  • 18
  • 64
  • 82

6 Answers6

315

First you have to set this value on the application configuration file:

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

Then, to determine the file in which you want to save the output you can add the following code in the same .config file:

<configuration>
...

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

...
</configuration>

You can find a more detailed explanation under 'How do I enable log4net internal debugging?' in the log4net FAQ page.

Malice
  • 3,927
  • 1
  • 36
  • 52
David Espart
  • 11,520
  • 7
  • 36
  • 50
  • 10
    Just make sure that the process that your application is running under has rights to the path where you want to create your logging text file ( "C:\tmp\log4net.txt" in the example above ) – NMrt May 27 '15 at 13:26
  • this is the missing tip to find what is happening on my app thanks :D – NFRiaCowboy Sep 09 '16 at 16:28
  • 3
    Looks like the internal debugging doesnt have timestamps – snit80 Oct 17 '16 at 03:00
  • 4
    I changed from `C:\tmp\log4net.txt` to `C:\log4net.txt`, then it can generate text file. – Frank Myat Thu Jun 29 '17 at 07:15
  • 7
    Just a note I found the fun way. Make sure the `` is the first entry under ``. Otherwise you end up with an error. – Nick Jul 25 '17 at 13:59
  • 2
    This doesn't show the time for the entries in the log file. I've tried setting attributes `traceOutputOptions="TimeStamp"` and `traceOutputOptions="DateTime"` in the `add` tag but it changes nothing in the log file contents. Does someone know how to set up log4net to show the time for each line/entry in the trace log file? – Ulysses Alves Jan 08 '19 at 12:05
50

If you are using a log4net config file you can also turn on the debugging there by changing the top node to:

<log4net debug="true">

This will work once the config is reloaded and assuming your trace listener is setup properly.

Bolo
  • 1,494
  • 1
  • 19
  • 19
  • 8
    Once this flag is enabled - you'll be able to see the diagnostic logs from log4net within Visual Studio's Output window – Naren Feb 22 '19 at 23:49
26

In addition to the above answer, you can use this line to see the log in realtime instead of the c:\tmp\log4net.txt output.

log4net.Util.LogLog.InternalDebugging = true;

For example, in a console app, you can add this and then watch the output in realtime. It's good for debugging log4net in a small test harness to see what's happening with the appender you're testing.

JP Toto
  • 1,032
  • 9
  • 10
10

Make sure the root application where your entry point is logs something to log4net. Give it one of these:

private static ILog logger = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
    logger.InfoFormat("{0} v.{1} started.", Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version.ToString());

With 2.0.8, I had an interesting situation. I created a library project and a test exe project that would demo it's capabilities. The library project was set up to use Log4net as was the exe project. The exe project used the assemblyinfo attribute to register the config, yet I was getting no logging output to either the console or the log file. When I turned on log4net internal debug logging, I got some internal messages written to the console, but still none of my normal logs. No errors were being reported. It all started working when I added the above code to my program. Log4net was otherwise setup correctly.

K0D4
  • 2,373
  • 1
  • 27
  • 26
  • 2
    saved mine as well :-) – boggy Feb 20 '19 at 22:32
  • uh, I had the same problem, it appears there are some requirements on assembly that is using log4net first - not sure what are they exactly, might be the referenced libraries or things that are used in the config (in my case I use a custom appender, so it's probably that - assembly that logs first, must also include the custom code needed when loading config). – nazgul Nov 13 '20 at 16:42
  • Thanks so much, this helped me as well. That's quite a gotcha! – EM0 Mar 08 '21 at 13:50
2

If the internal log doesn't give you enough information, it's very easy to build and debug the source code. If you don't want to mix this up with your development project, add a simple console application which just logs a message, copy your project's log4net.config to this application, and debug the class in question.

StuartN
  • 329
  • 1
  • 9
2

In log4net 2.0.8 it seems not to be possible to make the logging with log4net in a separate DLL. If I tried this, the results are very strange: No logging is performed anymore. And the initialization of log4net with the debug option shows no Errors.

Like K0D4 said, you should have a reference to log4net in your main-module and should it call once on the start of the Programm and all is fine.

In the next version of log4net, this bug will be probably be fixed.

Prajakta Kale
  • 392
  • 3
  • 19
olaf870
  • 31
  • 3