4

I need to log this because my custom appender API is growing and requires tracking of possible errors and various states.


Using log within a custom appender will cause a recursive error being thrown I believe.

Is there any good/best practice to log info/errors within a custom appender?

A semaphore lock with FIleStream and FileWriter seems ok but it will lock up resources and slow the process if multiple processes are contending to write on the same log file.


Official source : http://logging.apache.org/log4net/release/faq.html

As suggested by stuartd, LogLog can be used for internal debugging.

First in ~.config file of the project, 2 things must be set.

1)

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

This enables internal debugging of log4net/j

2)

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

This enables windows machine to trace. 2 things to make sure.
a) the application folder needs to be there because it doesn't check if folder exists for the log file. If the folder is not there the file won't be created.
b) administrator rights of this program is required else the file cannot be written.

3)

log4net.Util.LogLog.Debug(null, "testing internal debug");

In the custom appender, simply use this to do internal logging. For the input parameter {0} i am not sure why it requires obj Type. I will find out why and what is it's purpose.

  • Sad.. sorry SO Thread found that has similar solution. http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems – user2670633 Jul 17 '15 at 10:10
  • Looking at the source code, the type passed to LogLog debug is the type of the originating class - which makes sense as otherwise you wouldn't know where it came from, so you would call `log4net.Util.LogLog.Debug(this.GetType(), "testing internal debug");` – stuartd Jul 17 '15 at 13:45

1 Answers1

4

This should be done using the LogLog class (log4net docs, log4j docs)

This is from the log4net docs, but the log4j ones are very similar:

Log4net components cannot make log4net logging calls. However, it is sometimes useful for the user to learn about what log4net is doing.

To view the output you will need to enable internal logging - how to do this depends on which platform you're using.

Rob Church
  • 6,783
  • 3
  • 41
  • 46
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Thank you, LogLog can work with internal logging activated if Trace is enabled and internal logging is set to true in the ~.config file of the project. http://logging.apache.org/log4net/release/faq.html I cannot upvote you because my reputation is still too low. I clicked your post +1 already. – user2670633 Jul 17 '15 at 09:50