2
votes

I am trying to configure log4net for logging via Azure. However, once the logs make it to the storage the log source is attached to the log message-

WorkerRole.WorkerRole: Starting Role Test

I need an exact log format, however the log4net prepends WorkerRole.WorkerRole: to the message.

Here is the config I am using for the log4net setup-

<log4net>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <!-- can be any pattern you like -->
        <conversionPattern value="%message" />
      </layout>
    </appender>
    <!-- does not have to be at the root level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="TraceAppender" />
    </root>
  </log4net>
1

1 Answers

2
votes

I came across this same issue.

Looking at the Trace Appender in a debug session I could see that TraceAppender.Category = "%logger" which according to the documentation

Defaults to %logger which will use the logger name of the current LoggingEvent as the category parameter.

Further digging found this logged on the Log4Net bug tracking

TraceAppender : Add switch to disable using logger name as trace category

The item is marked as fixed, although it isn't clear exactly what you add to the config file. A bit of testing with some of the suggestions on the conversation thread found that you can just set the category to what you want, so if you don't want anything, then just add this to your config:

<category value="" />

Make sure it is a child of the appender tag, e.g.

<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
  <category value="" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level - %message%newline" />
  </layout>
</appender>

Alternatively I suppose you could try creating a new appender derived from log4net.Appender.TraceAppender and override the Category property to force it to your own value.