views:

855

answers:

2

I didn't want to make the title too long but this question specifically refers to running an NServiceBus Generic Host as a Windows Service (thanks to TopShelf) configured to run as Local System (on a Vista machine)

In a previous question I explain why I decided to adapt the PubSub sample to run as a Windows Service so that I can easily stop and start the service to fully prove to myself that NServiceBus was doing what it was supposed to do.

For some reason I can't get Log4Net to log anything to disk, so this could well just be a Log4Net (newbie) configuration problem?

Below is my brute-force attempt to get some sort of tracing going - all I'm getting so far is files writen as follows:

C:\logs\<-GUID->log4net.log

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, NServiceBus.Core"/>
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <!-- in order to configure remote endpoints use the format: "queue@machine" 
       input queue must be on the same machine as the process feeding off of it.
       error queue can (and often should) be on a different machine.
  -->

  <MsmqTransportConfig
    InputQueue="worker2"
    ErrorQueue="error"
    NumberOfWorkerThreads="1"
    MaxRetries="5"
  />

  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="Messages" Endpoint="messagebus" />
    </MessageEndpointMappings>
  </UnicastBusConfig>

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

  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="c:\logs\Subscriber2.log" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <datePattern value="yyyyMMdd" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>

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

    <appender name="EventLogAppender" type="log4net.appender.eventlogappender">
      <applicationname value="Subscriber2.EndPointConfig_v1.0.0.0" />
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>

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


    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="EventLogAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="TraceAppender" />
    </root>

  </log4net>

  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
          name="textWriterTraceListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="c:\logs\log4net.log" />
      </listeners>
    </trace>
  </system.diagnostics>

</configuration>
+1  A: 

It looks as if your other two appenders are throwing exceptions while trying to log. I suspect the %property{NDC} in your patterns - remove them from the pattern and try again.

If your pattern contains %property{X} then you need to set a property with key "X" using code such as

ABC.Properties["X"] = /* some value */

where ABC is either a LoggingEvent instance or ThreadContext or GlobalContext.

I don't know if you've set properties with key "NDC", but I suspect not...

Vinay Sajip
Thanks Vinay. I have removed these and fixed another error in the naming of the rolling file appender but I am still not getting any logging. I am debugging the apps in Visual Studio and it's stepping over the Logger.Info() lines OK. Very strange...
rohancragg
+3  A: 

NSB will not pick up log settings from config files as default. To do this implement IConfigureLogging in your endpoint config class.

More info here:

http://tech.groups.yahoo.com/group/nservicebus/message/3655

Hope this helps!

/Andreas

Andreas Öhlund
Whoa Andreas, thanks a million! This was spot on. I had to add the marker interface IWantCustomLogging - so obvious once you see it. Maybe I should have tried all the samples as is before trying to adapt one of them ;-)
rohancragg