如何为NServiceBus通用主机启用日志记录

时间:2009-09-28 19:43:24

标签: log4net nservicebus topshelf

我不想让标题太长,但这个问题特别指的是运行NServiceBus通用主机作为Windows服务(感谢TopShelf)配置为作为本地系统运行(在Vista机器上)

a previous question中,我解释了为什么我决定调整PubSub示例以作为Windows服务运行,以便我可以轻松地停止并启动服务,以充分证明NServiceBus正在做它应该做的事情

由于某种原因,我无法让Log4Net将任何内容记录到磁盘上,所以这可能只是一个Log4Net(新手)配置问题?

下面是我蛮力尝试进行某种追踪 - 我到目前为止所有文件都写成如下:

  

C:\日志\< -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>

2 个答案:

答案 0 :(得分:7)

NSB不会默认从配置文件中获取日志设置。为此,请在端点配置类中实现IConfigureLogging。

更多信息:

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

希望这有帮助!

/安德烈亚斯

答案 1 :(得分:1)

在尝试登录时,看起来你的其他两个appender正在抛出异常。我怀疑你的模式中有%property{NDC} - 从模式中删除它们然后再试一次。

如果您的模式包含%property {X},那么您需要使用代码(例如

)设置键为“X”的属性
ABC.Properties["X"] = /* some value */

其中ABC是LoggingEvent个实例或ThreadContextGlobalContext

我不知道您是否使用关键字“NDC”设置了属性,但我怀疑不是......