NServiceBus + Common.Logging + NLog

标签 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 部分。

谢谢!

最佳答案

NServiceBus 有自己的 Common.Logging 版本,该版本已合并并内化到 Core dll 中,用于配置自己的日志记录(使用 log4net)。当它启动时,它会发现 app.config 中的配置部分并尝试加载它。但是,您的配置部分(称为“logging”)指向外部 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。

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

关于NServiceBus + Common.Logging + NLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115376/

相关文章:

asp.net-mvc - 需要时在 CQRS 中提供同步行为?

c# - 单元测试自定义 NLog 目标

c# - ILogger 不返回 JSON 格式的消息和对象

c# - NLog 使用 NLog.config 动态更改文件名

azure - NServiceBus 和 Azure 长时间运行处理程序模式

msmq - 使用 NServiceBus 获取 MSMQ 消息的 Id 属性

.net - NLog每日归档大小限制

common.logging - 无法加载文件或程序集 Common.Logging

log4net - 如何在Quartz.Net中从Log4Net切换到NLog?

azure - ServiceuBusTrigger 从 KeyVault 读取服务总线连接字符串