Question

I need to catch log4net exceptions (its own exceptions not app exceptions logged by it). I wish there's a way of doing it this way:

Try
    _logger.Info(response)
Catch ex As Exception
    Excepciones.HandleException(ex, "PolicyCore")
End Try

I have this code implemented and there's no errors in compilation but i force log4net to have an error (pointing to a non existing database in the config file) and nothing is threw.
I've tried the listener aproach:

 <appSettings>
        <add key="log4net.Internal.Debug" value="true" />
    </appSettings>
    <system.diagnostics>
       <trace autoflush="true" indentsize="4" >
         <listeners>
           <add name="textWriterTraceListener" 
               type="System.Diagnostics.TextWriterTraceListener"
               initializeData="C:\\temp\\log4net.txt" />
           <remove name="Default" />
         </listeners>
       </trace>
    </system.diagnostics>

and it's writing the errors to log4net.txt, the forced ones i mean.

This last aproach has a couple of drawbacks: it won't append every error to the file, if the error is the same it doesn't write it, i can't get the listener to write every error to that file, only one (I don't know how to fully configure the trace listener, it might be that). Thus it won't append the date and hour to every line wich is a necesity for me. Finally i can't give structure to it (xml).

Even if the listener work i need to use the try/catch aproach, since i'm using ExceptionHandling from Enterprise library to log the errors in my app.

Anyone who has faced the problem and/or has a solution?

Was it helpful?

Solution

Log4net is designed not to throw any exceptions. This seems to be a good idea because it would be very bad indeed if your application fails because it cannot log. So if you need to log certain messages and need to be sure that this worked, then you should not use a log framework, but rather implement this as part of your "business logic".

You mention that an exception is logged only once. I think that is not true: Log4net usually disables appenders that do not work and thus there is only one exception. The AdoNetAppender for instance can be configured to re-connect to the database in case of a failure in which case you would see the same exception multiple times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top