Logging / error handling inside the log4net user application

We have an internally developed registration system, and we wanted to get the advantages of log4net, for example, the ability to use several applications simultaneously. We decided to upgrade the legacy logging system to the user app log4net. This worked fine when the legacy system was configured correctly, but the legacy registrar actually writes to the WCF client (which makes it slow for verbose logging, the reason we wanted to use other log4net applications), and recently the WCF configuration wasn’t quite like that . When this happened, the application seemed to work fine, but obviously the user appender for the legacy system did not work. Other applications worked fine, but there was no output from a regular legacy application and no error messages indicating a problem.

What is the proper way to handle the "recursive" logging that is logged inside the log4net user application? I tried a naive solution: grab log4net from its static object and write inside the application:

public class MyCustomAppender : AppenderSkeleton { protected override void Append(log4net.Core.LoggingEvent loggingEvent) { try { // something which throws here... } catch (Exception ex) { log4net.LogManager.GetLogger(this.GetType()).Error(this.GetType().ToString() + ": error during append", ex); } } } 

It was a failed failure: "System.Threading.LockRecursionException: Recursive read locks are not allowed in this mode." Fair; it was a naive decision.

I also tried:

 this.ErrorHandler.Error(this.GetType().ToString() + ": error during append", ex); 

in the exception handler, as this seemed possible, it might be right. Nothing obvious has happened.

Is there a way to log errors through log4net inside the application? Or do I need to do something like write hard code to the Windows event log directly? Obviously, there is a great danger of an infinite recursive descent, but I would have thought that there is some way to make the operator understand that one of the participants was unable to perform.

Jr

+5
source share
2 answers

Of course, as soon as I posted the question, I remembered something. log4net has a debug switch inside appSettings. I used this code:

 this.ErrorHandler.Error(this.GetType().ToString() + ": error during append", ex); 

inside the catch block, then set the magic setting to:

 <appSettings> <!--this is wonderful magic--> <add key="log4net.Internal.Debug" value="true" /> </appSettings> 

Now ConsoleAppender (another application that I used) displays a message about the exception of the user application.

Jr

+3
source

Make sure you have this line at the top: using System.Threading;

0
source

All Articles