0

We've been using Database appender (AdoNetAppender) in my company for a while. We're working on a deploy right now and not seeing any errors being persisted to the database. one thought we have is that the Database Connection is being blocked by a firewall, but you can already see where this goes into a circle of pain.

I added a RollingFileAppender, a FileAppender, tried using a bufferingForwarder, and have tried everything on stackoverflow's green world to solve this problem and absolutely nothing is changing. I even removed the reference to the AdoNetAppender and now it's just not logging errors at all, whereas with the AdoNetAppender it was logging errors twice -- once for AdoNetAppender, once for FileAppender.

My xml code is:

    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="AdoNetAppender"/>
    </root>
    <appender name="AdoNetAppender">
        <!--working xml. No need to post this information-->
    </appender>
    <appender name="BufferingForwarder" type="log4net.Appender.BufferingForwardingAppender">
        <bufferSize value="5" />
        <lossy value="false" />
        <appender-ref ref="fileAppender" />
        <!-- or any additional appenders or other forwarders -->
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\log4net.log"/>
      <datePattern value="yyyy-MM-dd'-FULL.log'" />
      <appendToFile value="true"/>
      <preserveLogFileNameExtension value="true"/>
      <rollingStyle value="Size"/>      
      <maximumFileSize value="250KB"/>      
      <maxSizeRollBackups value="-1"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>

My c# code is:

(using Logger: ExceptionFilterAttribute)

private static ILog Log;
private static ILog FileLog;
private static bool _isConfigured = false;

private static void ConfigureLogger() 
{
    if (_isConfigured) return;
    XmlConfigurator.Configure();
    FileLog = LogManager.GetLogger("RollingFileAppender")
    Log = LogManager.GetLogger("AdoNetAppender")
    _isConfigured = true;
}
public static void Error(string message, Exception e) {
    ConfigureLogger();
    Log.Error(message, e);
}
public override void OnException(HttpActionExecutedContext context) 
{
    Error("Unexpected error occurred", context.Exception);
    ConfigureLogger();
    if (FileLog != null)
        FileLog.Error("Unexpected Error Occurred", context.Exception);
}
user3654055
  • 178
  • 14
  • Did it work at first, then stopped working or did it **never** work at all for this particular application? Also, have you checked the Windows event log? – JuanR Jun 25 '18 at 14:06
  • Configure log4net's [own internal debugging](https://stackoverflow.com/a/756241/4137916) to see if it has anything interesting to say. – Jeroen Mostert Jun 25 '18 at 14:09
  • 1
    Also, as an aside, it is not necessary to execute `.GetLogger()` only after you've configured log4net. Logger objects can be created in advance and they'll work correctly when the configuration is applied; the usual `static readonly ILog FileLog = LogManager.GetLogger("...")` pattern is attractive because it avoids code of the form `if (logger != null) ...` – Jeroen Mostert Jun 25 '18 at 14:19
  • RollingFileAppender logging has never worked. AdoNetAppender has always worked, but the company that hosts our ProductionServer and StagingServer recently underwent some firewall changes. Now our Staging Server has a 500.0 Error and we can't figure out why because nothing is logging. Windows Event log shows nothing, HttpRequestLog tells us it's a 500.0 error meaning either ISAPI Module or else internal Execution issue. – user3654055 Jun 25 '18 at 14:23
  • @JeroenMostert if GetLogger is not necessary, how do I assign a logger to FileLog or Log without assigning it to FileLog or Log? From what you're saying (and it would make sense if so) it sounds like assigning it the proper way would probably resolve my issue. – user3654055 Jun 25 '18 at 14:25
  • No, calling `LogManager.GetLogger` is very much necessary -- my point was that you can do this as part of an initializer on the same line as the declaration, you don't need to do this in a separate `ConfigureLogger` method. There is (usually) no functional difference (except for allowing you to omit `null` checks). `XmlConfigurator.Configure()` should be called once in your startup code, but acquiring loggers can be done at the places where they are needed. – Jeroen Mostert Jun 25 '18 at 14:27
  • 1
    ...oh, and I missed the fact that you're calling loggers with *appender* names. Loggers are not appenders! Your configuration has only one logger: the root logger. Check the log4net configuration examples for more information -- you've got your wires crossed with regards to what to log where. If you want to log exclusively to one location or another, you need to use loggers with their own appenders, and either not add any appenders to the root logger, or configure the loggers to be non-additive. – Jeroen Mostert Jun 25 '18 at 14:30
  • A 500 error should be logging stuff on the Windows log. Change the customErrors element in your config so you can get detailed information about the error on screen as opposed to the generic 500 message. – JuanR Jun 25 '18 at 14:46
  • It's possible the service account doesn't have rights to write to a file. If that's the case then Log4Net will quietly fail and it will look like nothing happened. – Duston Jun 25 '18 at 15:11
  • What if the file doesn't exist? Also what if the application is a child application? Would it be logging to the parent Location? Could it be that the Child Application doesn't have create access to the parent location directory and therefore can't create the file? – user3654055 Jun 25 '18 at 15:34
  • So it seems to be a permissions issue. Later I'll post the code I used. I don't know if I'm hammering a square-shaped peg into a round hole with this, but it works. – user3654055 Jun 29 '18 at 15:06

0 Answers0