首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NServiceBus + Common.Logging + NLog

NServiceBus + Common.Logging + NLog
EN

Stack Overflow用户
提问于 2012-06-20 16:13:30
回答 1查看 3K关注 0票数 2

我正在尝试弄清楚如何在组合withCommon.Logging中使用NServiceBus。我就是不能让它运行起来。我尝试开发一个仅用于教育目的的小型演示应用程序。

我所做的是:

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

代码语言:javascript
复制
<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时:

代码语言:javascript
复制
var bus = Configure.With().DefaultBuilder()
                             .XmlSerializer()
                             .MsmqTransport()
                             .IsTransactional( true )
                             .UnicastBus()
                             .MsmqSubscriptionStorage()
                             .CreateBus()
                             .Start( () => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install() );

我得到了这个例外:

代码语言:javascript
复制
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部分的日志记录。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-21 03:50:16

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

代码语言:javascript
复制
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />

NSB希望它看起来是什么样子:

代码语言:javascript
复制
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, NServiceBus.Core" />

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

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

我可能会补充说,在不再使用Common.Logging的NSB4.0中,这个问题将会消失。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11115376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档