我正在尝试弄清楚如何将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() );
我得到这个异常:
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:DevelopmentkatasConsoleApplication1ConsoleApplication1Program.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 中的配置部分并尝试加载它。但是,您的配置部分(称为"日志记录")指向外部 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。