0

I'm implementing a logging solution with Log4Net for a Windows NT Service. I can flip a switch in a configuration file to start logging information to the file system. So far I've been able to accomplish this by having a rolling file appender and having log4net "Watch" the configuration file.

What I've notice is that Log4Net will create empty log files as soon as the service starts, even if all logging is turned off and I don't intend to log.

I can't find much information on this topic other than this post:

How to disable creation of empty log file on app start?

My concern is that someone could set up the service with a very low level set of permissions that wouldn't even have access to create the log files on the file system. I don't want to incur the performance penalty of having exceptions thrown every time I hit a logging statement even if I didn't intend to log.

I've wrapped each logging statement with check to see if the logging level is enabled before attempting to log, but I'm still not sure if in the inner workings of log4net whether or not exceptions are going to still be thrown if the file configured in the appender wasn't created.

if (logger.IsDebugEnabled)
{
    logger.DebugFormat(format, traceMessage);    
}

Does anyone know what log4net will do if it can't create the log files initially?

Here is a little information about my configuration. Logging works fine when it is turned on, I'm just worried about permissions issues.

Attribute on my logging class to watch for config changes:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Logging.config", Watch = true)]

Appender:

  <appender name="Test" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value=".\\AppLogs\\_test.%appdomain.log" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ALL" />
      <levelMax value="FATAL" />
      <acceptOnMatch value="false"/>
    </filter>
    <filter type="log4net.Filter.LoggerMatchFilter">
      <loggerToMatch value="Test" />
    </filter>
    <appendToFile value="true" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="6MB" />
    <rollingStyle value="Size" />
    <staticLogFileName value="true" />
    <filter type="log4net.Filter.DenyAllFilter" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level - %message%newline" />
    </layout>
  </appender>
Community
  • 1
  • 1
Loathian
  • 1,177
  • 1
  • 14
  • 20
  • I'm not sure if this was true in 2011, but at least for recent versions of Log4Net, it checks internally whether the given log level is enabled before actually trying to log anything. The functions like IsDebugEnabled are only useful if it is CPU/Resource-intensive to generate the data being sent to the logger. Related, functions like DebugFormat have the dual purpose of being concise but also that it doesn't have to do the actual string.Format unless the log level is enabled. – ShawnFumo Apr 16 '15 at 21:07

1 Answers1

1

"My concern is that someone could set up the service with a very low level set of permissions that wouldn't even have access to create the log files on the file system. " - on service start up detect your minimum set of permisions required is present and throw an exception (or otherwise handle) if they are not.

I don't think this is a logging issue per se; it's more an issue of whether or not your service has its required permissions.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • That is a good suggestion. I was reading in this post that I will want to make sure I'm writing to the appropriate location for Windows 7 and Windows Server 2008 http://stackoverflow.com/questions/468989/how-to-specifiy-common-application-data-folder-for-log4net ... is there a good pattern for notifying the user why the service cannot start? Most likely if I can't write to the ProgramData location how do I give the user a message about why the service won't start? – Loathian Jan 11 '11 at 04:00
  • Mitch, I went ahead and accepted your answer as it has been a couple of days and there were no other postings. Thanks for the feedback. – Loathian Jan 13 '11 at 19:00