NServiceBus + Common.Logging + NLog

时间:2012-06-20 08:13:30

标签: nservicebus nlog common.logging

我正在试图弄清楚如何将NServiceBus与Common.Logging结合使用。我只是无法让它运行。我尝试开发一个仅用于教育目的的小型演示应用程序。

我做的是:

1)创建一个简单的控制台应用程序并导入Common.Logging和Common,Logging.NLog,添加了一些Info消息并添加了一个App.Config文件:

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
        <arg key="level" value="DEBUG" />
        <arg key="showLogName" value="true" />
        <arg key="showDataTime" value="true" />
        <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
      </factoryAdapter>
    </logging>
  </common>
</configuration>

这很好用。但是,当我包括NServiceBus时:

var bus = Configure.With().DefaultBuilder()
                             .XmlSerializer()
                             .MsmqTransport()
                             .IsTransactional( true )
                             .UnicastBus()
                             .MsmqSubscriptionStorage()
                             .CreateBus()
                             .Start( () => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install() );

我得到了这个例外:

System.TypeInitializationException was unhandled
  Message=The type initializer for 'NServiceBus.Configure' threw an exception.
  Source=NServiceBus.Core
  TypeName=NServiceBus.Configure
  StackTrace:
       at NServiceBus.Configure.With()
       at ConsoleApplication1.Program.Main(String[] args) in D:\Development\katas\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Common.Logging.ConfigurationException
       Message=ConfigurationReader Common.Logging.Configuration.DefaultConfigurationReader returned unknown settings instance of type Common.Logging.Configuration.LogSetting
       Source=NServiceBus.Core
       StackTrace:
            at Common.Logging.Configuration.ArgUtils.Guard[T](Function`1 function, String messageFormat, Object[] args)
            at Common.Logging.Configuration.ArgUtils.Guard(Action action, String messageFormat, Object[] args)
            at Common.Logging.LogManager.BuildLoggerFactoryAdapter()
            at Common.Logging.LogManager.get_Adapter()
            at Common.Logging.LogManager.GetLogger(String name)
            at NServiceBus.Configure..cctor()
       InnerException: System.ArgumentOutOfRangeException
            Message=Type 'Common.Logging.Configuration.LogSetting, Common.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e' of parameter 'sectionResult' is not assignable to target type 'Common.Logging.Configuration.LogSetting, NServiceBus.Core, Version=3.2.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c'
Parameter name: sectionResult
Actual value was Common.Logging.Configuration.LogSetting.
            Source=NServiceBus.Core
            ParamName=sectionResult
            StackTrace:
                 at Common.Logging.Configuration.ArgUtils.AssertIsAssignable[T](String paramName, Type valType, String messageFormat, Object[] args)
                 at Common.Logging.Configuration.ArgUtils.AssertIsAssignable[T](String paramName, Type valType)
                 at Common.Logging.LogManager.<>c__DisplayClass3.<BuildLoggerFactoryAdapter>b__1()
                 at Common.Logging.Configuration.ArgUtils.<>c__DisplayClass13.<Guard>b__12()
                 at Common.Logging.Configuration.ArgUtils.Guard[T](Function`1 function, String messageFormat, Object[] args)
            InnerException: 

我已经尝试过在StackOverflow和其他组中建议的几件事,但我无法让它工作。任何人都可以就如何处理此问题提供一些建议吗?或者提供一个简单的例子?

这个设置不应该是花哨的,对吗?我现在甚至不需要记录NServiceBus部分。

谢谢!

1 个答案:

答案 0 :(得分:3)

NServiceBus拥有自己的Common.Logging版本,该版本被内化并内化到Core dll中,用于配置它自己的日志记录(使用log4net)。启动时,它会发现app.config中的配置部分并尝试加载它。但是,您的配置部分(称为“日志记录”)指向外部Common.Logging dll:

<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />

NSB希望它看起来像:

<section name="logging" type="Common.Logging.ConfigurationSectionHandler, NServiceBus.Core" />

因此,NSB会抛出错误。这肯定是NSB中的一个错误。我建议你在他们的github项目上创建一个问题。

虽然在您的情况下可能不是最佳解决方案,但解决它的最快方法是删除app.config中的日志记录部分并在代码中配置Common.Logging。或者,完全跳过使用Common.Logging并直接使用NLog。

我可能会补充一点,这个问题将在NSB 4.0中消失,而不再使用Common.Logging。