0
votes

I have an Azure worker role that I wish to update the logging (the project had existing logging, although I'm not convinced it was ever working). It is set up to use Common.Logging, but with Log4Net. I have set up 2 appenders in Log4Net -- one to ElasticSearch and one to a rolling file -- neither of which are logging any output.

In the worker role's app.config, I have configured log4net, followed by Common.Logging:

...
<log4net>
<appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4stash">
  <Server>localhost</Server>
  <Port>9200</Port>
  <IndexName>log_test_%{+yyyy-MM-dd}</IndexName>
  <IndexType>LogEvent</IndexType>
  <Bulksize>2000</Bulksize>
  <BulkIdleTimeout>10000</BulkIdleTimeout>
  <IndexAsync>False</IndexAsync>
  <ElasticFilters>
    <Add>
      <Key>My Data Origin</Key>
      <Value>Worker Role</Value>
    </Add>
  </ElasticFilters>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="C:\tmp\log4net_rollingfile.log" />
  <appendToFile value="true" />
  <maximumFileSize value="100KB" />
  <maxSizeRollBackups value="2" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%level %thread %logger - %message%newline" />
  </layout>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="ElasticSearchAppender" />
  <appender-ref ref="RollingFile" />
</root>
</log4net>

<common>
<logging>
  <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
    <arg key="configType" value="INLINE" />
  </factoryAdapter>
</logging>
</common>

I would tend to think that something is off in the configuration. However, the RollingFile appender does indeed create c:\tmp\log4net_rollingfile.log, so I'm at a loss to explain why nothing is actually written to the file. Also, no data is sent to ElasticSearch.

Having turned on the Log4Net diagnostics with "log4net.Internal.Debug", I can see that the appenders are getting loaded:

log4net: Adding appender named [ElasticSearchAppender] to logger [root].
log4net: Adding appender named [RollingFile] to logger [root].
2

2 Answers

1
votes

There were 2 things I ended up doing to fix this issue. The first may not have been necessary, but just in case, I'm including it here. The second -- well, that's just a case of modern development being unfortunately helpful. Thankfully, Common.Logging's source code is publicly available, which was very important in tracking down the problem in the debugger.

First (and possibly unnecessary for the resolution at hand), the modern way of setting up your log4net factoryAdapter in common logging is to use a library specific to your current version of log4net. There wasn't one for the version I am using (log4net 1.2.15), but using the Log4NetLoggerFactoryAdapter from Common.Logging.Log4Net1213 worked:

<common>
<logging>
  <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213, Version=3.3.1.0, Culture=neutral">
    <arg key="configType" value="INLINE" />
  </factoryAdapter>
</logging>
</common>

Second, the real nitty gritty of the problem, was app.config. Not the app.config I copied in the original question, above, but specifically, app.Debug.config. My brain completely ignored the little triangle to the left of app.config that suggests multiple resources involved with app.config. in app.Debug.config, my factoryAdapter was being completely overridden with a Common.Logging.Simple.TraceLoggerFactoryAdapter. Hence, regardless of what I put in the main app.config, I was never writing logs to the expected locations. No idea why the empty file was being created for the RollingFile appender, but that's another problem for another day.

0
votes

Try writing the log into the Web/Worker Role directory tree.