简体   繁体   中英

Log4Net Stops logging

I have a utility which runs all day for data conversions and synchronization.

If the app stops running temporarily, it's not a big deal, but it has to log it's actions. I would rather the app fail than have it not log. I have come across a few posts which touch on the subject, but I cannot get any of the solutions to work.

I need to set up a way to stop the application from running (depending on an app setting) and send out a warning email if Log4Net stops logging.

I have this for my appender:

<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
  <errorHandler type="SomeApp.PresentationLayer.Log4NetErrorHandler" />
  <file type="log4net.Util.PatternString" value="H:\SomeApp-%property{RegNo}-log.txt" />
  <appendToFile value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <header value="[START]" />
    <footer value="[END]" />
    <conversionPattern value="%date %-5level - %message%newline" />
  </layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
  <level value="DEBUG" />
  <appender-ref ref="LogFileAppender" />
  <!-- <appender-ref ref="A" /> -->
</root>

And here's my Error Handler:

namespace SomeApp.PresentationLayer
{
class Log4NetErrorHandler : IErrorHandler
{

    public bool HandleError(Exception ex)
    {
        //Trace.TraceError(ex.ToString());
        ExceptionUtil.GetAndLogMessage(ex, "Log4Net Error in SomeApp", false, true);

        return false;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        // Shield the unknown exception
        FaultException faultException = new FaultException(
            "Server error encountered. All details have been logged.");
        MessageFault messageFault = faultException.CreateMessageFault();

        fault = Message.CreateMessage(version, messageFault, faultException.Action);
    }
}
}

No matter what I try, the exception handler never fires. I have the log file set to a mapped drive. While it's logging I disconnect the drive and it never throws an exception to my exception handler. Anyone see what I'm missing?

I'm using c# .net 3.5 log4net 1.2.10

Thanks for any help in advance.

I actually have not used log4net errorHandlers and may be completely off here, but my intuition tells me that the errorHandler is meant to configure the handling of log4net exceptions, not application exceptions, and that there is so little information available about this subject that a bogus blog post about the errorHandler parameter probably confused you. From the SDK documentation:

Implements log4net's default error handling policy which consists of emitting a 
message for the first error in an appender and ignoring all subsequent errors. 

Not very helpful, I admit, but it does not seem to do what you think. You have to properly handle application exceptions in your application and log the message yourself to see it in the log.

And there is another question here that may provide additional clues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM