开发者

What happens if Log4Net doesn't have permission to access the file system

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 val开发者_Go百科ue="%date [%thread] %-5level - %message%newline" />
    </layout>
  </appender>


"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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜