简体   繁体   中英

Spring.Net. ContextRegistry.GetContext() exception, cannot configure Common.Logging

I cannot get the IApplication context for my console app

I get an exception with this detail: The type initializer for 'Spring.Context.Support.ContextRegistry' threw an exception.

With inner exception: Could not configure Common.Logging from configuration section 'common/logging

There's clearly something basic I've not hooked up, but I'm not sure what.

using Spring.Context;
using Spring.Context.Support;

namespace ConsoleApplication4
{
   class Program
   {
       static void Main(string[] args)
       {
          IApplicationContext ctx = ContextRegistry.GetContext();
       }
   }
}

And my app.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.                  </description>
    </objects>
  </spring>
</configuration>

The type initializer for 'Spring.Context.Support.ContextRegistry' threw an exception

This is valueable information and Spring.net is really good at providing additional information. Whenever Spring.net throws something, be sure to read the InnerException .

WHen I edit my config I get the messages: Could not find schema information for the attribute #. for # = 'uri', 'context', 'resource' and 'spring'

This is normal if you didn't install the schemas. You can download the schemas at their site and find additional information in their docs . Please note that this is optional and spring runs without those schemas.

Spring.Net uses Common Logging as facility for logging, you have to add the logging configuration to your app.config and the proper library to the referenced assemblies.

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

http://netcommon.sourceforge.net/docs/1.2.0/reference/html/logging.html#logging-declarative-config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM