Nservicebus n服务总线&x2B;通用。日志记录+;NLog

Nservicebus n服务总线&x2B;通用。日志记录+;NLog,nservicebus,nlog,common.logging,Nservicebus,Nlog,Common.logging,我正在尝试找出如何将NServiceBus与common.Logging结合使用。我就是不能让它运行。我尝试开发一个小的演示应用程序,只是为了教育目的 我所做的是: 1) 创建一个简单的控制台应用程序,导入Common.Logging和Common,Logging.NLog,添加一些信息消息并添加App.Config文件: <configuration> <configSections> <sectionGroup name="common">

我正在尝试找出如何将NServiceBus与common.Logging结合使用。我就是不能让它运行。我尝试开发一个小的演示应用程序,只是为了教育目的

我所做的是:

1) 创建一个简单的控制台应用程序,导入Common.Logging和Common,Logging.NLog,添加一些信息消息并添加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() );
var bus=Configure.With().DefaultBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransaction(真)
.UnicastBus()
.MsmqSubscriptionStorage()
.CreateBus()
.Start(()=>Configure.Instance.ForInstallationOn().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: 
未处理System.TypeInitializationException异常 Message=NServiceBus.Configure的类型初始值设定项引发异常。 Source=NServiceBus.Core TypeName=NServiceBus.Configure 堆栈跟踪: 在NServiceBus.Configure.With()处 在D:\Development\katas\ConsoleApplication1\ConsoleApplication1\Program.cs中的ConsoleApplication1.Program.Main(字符串[]args)处:第17行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 InnerException:Common.Logging.ConfigurationException Message=ConfigurationReader Common.Logging.Configuration.DefaultConfigurationReader返回了类型为Common.Logging.Configuration.LogSetting的未知设置实例 Source=NServiceBus.Core 堆栈跟踪: at Common.Logging.Configuration.ArgUtils.Guard[T](函数'1函数,字符串messageFormat,对象[]args) 在Common.Logging.Configuration.ArgUtils.Guard(Action-Action,String-messageFormat,Object[]args) 在Common.Logging.LogManager.BuildLoggerFactoryAdapter()上 在Common.Logging.LogManager.get_Adapter()上 在Common.Logging.LogManager.GetLogger(字符串名称)处 在NServiceBus.Configure..cctor()上 InnerException:System.ArgumentOutOfRangeException 参数“sectionResult”的消息=类型“Common.Logging.Configuration.LogSetting,Common.Logging,Version=2.0.0,Culture=neutral,PublicKeyToken=af08829b84f0328e”不能分配给目标类型“Common.Logging.Configuration.LogSetting,NServiceBus.Core,Version=3.2.0.0,Culture=neutral,PublicKeyToken=9fc386479f8a226c” 参数名称:sectionResult 实际值为Common.Logging.Configuration.LogSetting。 Source=NServiceBus.Core ParamName=sectionResult 堆栈跟踪: 在Common.Logging.Configuration.ArgUtils.assertisSignable[T](字符串参数名,类型valType,字符串消息格式,对象[]args) 在Common.Logging.Configuration.ArgUtils.assertisSignable[T](字符串参数名,类型valType) 在Common.Logging.LogManager.c__DisplayClass3.b__1()上 在Common.Logging.Configuration.ArgUtils.c__DisplayClass13.b__u12()中 at Common.Logging.Configuration.ArgUtils.Guard[T](函数'1函数,字符串messageFormat,对象[]args) 内部异常: 我已经在StackOverflow和其他小组中尝试了一些建议,但我就是无法让它工作。有人能就如何处理这个问题提供一些建议吗?或者提供一个简单的例子

这样的设置不应该太花哨,对吧?我现在甚至不需要为NServiceBus部分记录日志


谢谢

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

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

NSB希望它看起来像:

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

因此,NSB抛出一个错误。这无疑是NSB中的一个bug。我建议您在他们的github项目上创建一个问题

虽然在您的情况下可能不是最理想的解决方案,但解决这个问题的最快方法是删除app.config中的logging部分并配置Common.logging代码。或者,完全跳过使用Common.Logging,直接使用NLog


我可以补充一点,这个问题在NSB 4.0中很常见。不再使用日志记录。

谢谢您的回答。我会一直努力让它工作,因为我认为这应该是一件容易的事情。正如你所建议的,我最终直接使用了NLog。我真的不喜欢它,但它现在正在运行,NServiceBus很高兴地抛出异常,但现在原因完全不同了:)