Question

I'm trying to figure out how to use NServiceBus in combination withCommon.Logging. I just can not get it running. I try to develop a small demo application just for educational purpose.

What I did was:

1) Create a simple console application and imported Common.Logging and Common,Logging.NLog, added some Info messages and added an App.Config file:

<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>

That worked just fine. But when I include NServiceBus:

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

I get this exception:

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: 

I've already tried several things suggested in StackOverflow and other groups, but I just can't get it working. Can anyone offer some advice on how to approach this? Or provide a simple example?

This setup shouldn't be to fancy, right? I don't even need logging for the NServiceBus part for now.

Thanks!

Était-ce utile?

La solution

NServiceBus has it's own version of Common.Logging that is ilmerged and internalized into the Core dll, that it uses to configure it's own logging (using log4net). When it starts up it discovers the configuration section in your app.config and tries to load it. However, your configuration section (called "logging") points to the external Common.Logging dll:

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

Where NSB wants it to look like:

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

Because of this, NSB throws an error. This is most certainly a bug in NSB. I suggest you create an issue on their github project.

While maybe not the most optimal solution in your case, the quickest way to work around it is to delete the logging section in app.config and configure Common.Logging in code. Or, skip using Common.Logging altogether and use NLog directly.

I might add that this problem will go away in NSB 4.0 where Common.Logging is not used anymore.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top