0

Case

Currently I'm in the process of deploying a WPF-application with log4net logging. In development I had no issues with the RollingFileAppender. However when deployed and started in a Citrix environment, the current user obviously has no permissions to write log files in the application directory.

Questions

  • Is it somehow possible to provide credentials to log4net and/or the RollingFileAppender itself to log with a service account?
  • If not possible, any ideas for a workaround (except for giving all users permissions on the directory)?

Thanks in advance!

4

2 回答 2

3

两年后,我再次遇到同样的问题,并找到了一个不错的内置解决方案。不再需要自定义附加程序!

此解决方案作为配置属性提供,log4net 调用securityContext并适用于每个附加程序。请参见下面的示例。

<log4net>
   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
       <filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="INFO" />
      </filter>

      <file type="log4net.Util.PatternString" value="LogFiles\%date{yyyyMMdd}\%property{log4net:HostName}_%env{username}" />
      <datePattern value=".yyyyMMdd'.log'" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="10" />
      <staticLogFileName value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%date [%3thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>

      <securityContext type="log4net.Util.WindowsSecurityContext">
         <domainName value="**myDomain**"/>
         <userName value="**myUser**" />
         <password value="**mySecretPassword**" />
      </securityContext>
   </appender>

   <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="ERROR" />
      </filter>

      <logName value="Application" />
      <applicationName value="**MyAppName**" />
      <Layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%date [%thread] %-5level %logger{1} - %message%newline" />
      </Layout>

      <securityContext type="log4net.Util.WindowsSecurityContext">
         <domainName value="**myDomain**"/>
         <userName value="**myUser**" />
         <password value="**mySecretPassword**" />
      </securityContext>
   </appender>

   <root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="EventLogAppender" />
   </root>
</log4net>
于 2014-08-07T08:42:40.337 回答
1

我不熟悉 Citrix 环境,但您可以创建自定义附加程序,从 RollingFileAppender 派生:

public class CitrixRollingFileAppender : RollingFileAppender
{
    public string Login { get; set; }
    public string Password { get; set; }    

    protected override void Append(LoggingEvent loggingEvent)
    {
        // use Login and Password here, or authenticate only once when appender created
        base.Append(loggingEvent);
    }
} 

和配置:

<appender name="citrix" type="YourNamespace.CitrixRollingFileAppender, YourAssembly">
  <file value="citrixlog.txt"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <login value="Foo"/>
  <password value="Bar"/>
  <layout type="log4net.Layout.SimpleLayout"/>
</appender>

更新:您可以覆盖此类方法,例如在配置 appender 后ActivateOptionsOnClose即将关闭时执行一些代码。

于 2012-04-10T09:23:30.133 回答