Question

I currently have a Windows Service that makes use of MassTransit. This Windows Service uses Log4Net for all logging, and now I want to also turn on Log4Net for internal MassTransit logging. But, I want to:

  • Log the MT logs to a different file than the Windows Service logs
  • Use a single (existing) Log4Net config file to configure both outputs

I have already added sbc.UseLog4Net() when initializing the Bus, and that works great. But, it is writing to the same file as my Windows Service logs.

I do realize I can call sbc.UseLog4Net(@"log4net.config") with a separate file to accomplish this.

What I really want is to be able to modify my current log4net.config file to accomplish this.

Was it helpful?

Solution

Sure, this isn't too hard. Setup two appenders in your log4net config.

  <appender name="RollingFile1" type="log4net.Appender.FileAppender">
    <file value="log1.log" />
    <appendToFile value="false" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
    </layout>
  </appender>
  <appender name="RollingFile2" type="log4net.Appender.FileAppender">
    <file value="log2.log" />
    <appendToFile value="false" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
    </layout>
  </appender>

Then just setup two loggers, one to each appender.

<logger name="MassTransit">
  <level value="INFO" />
  <appender-ref ref="RollingFile1" />
</logger>

<logger name="MyLogger">
  <level value="INFO" />
  <appender-ref ref="RollingFile2" />
</logger>

You just point each logger at the right file and call it a day. If you have questions about how any of these elements work, I'd hit up the manual: http://logging.apache.org/log4net/release/manual/configuration.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top