Castle Windsor在WinForm应用程序中使用log4net时出错

时间:2011-05-03 09:35:14

标签: winforms log4net castle-windsor

我在WinForm应用程序中使用Windsor Castle和log4net时出现此错误。

错误:

Could not convert from 'Castle.Services.Logging.Log4netIntegration.Log4netFactory,Castle.Services.Logging.Log4netIntegration,Version=2.5.1.0, Culture=neutral,PublicKeyToken=407dd0808d44fbdc' to System.Type - Maybe type could not be found

堆栈:

in Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\TypeNameConverter.cs:riga 91
in Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:riga 134
in Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:riga 162
in Castle.Facilities.Logging.LoggingFacility.GetLoggingFactoryType(LoggerImplementation loggerApi)
in Castle.Facilities.Logging.LoggingFacility.CreateProperLoggerFactory(LoggerImplementation loggerApi)
in Castle.Facilities.Logging.LoggingFacility.ReadConfigurationAndCreateLoggerFactory()
in Castle.Facilities.Logging.LoggingFacility.Init()
in Castle.MicroKernel.Facilities.AbstractFacility.Castle.MicroKernel.IFacility.Init(IKernel kernel, IConfiguration facilityConfig) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\Facilities\AbstractFacility.cs:riga 85
in Castle.MicroKernel.DefaultKernel.AddFacility(String key, IFacility facility) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel.cs:riga 320
in Castle.MicroKernel.DefaultKernel.AddFacility[T](Func`2 onCreate) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel.cs:riga 377
in Castle.Windsor.WindsorContainer.AddFacility[T](Func`2 onCreate) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 629
in IocWinFormTest.Installers.LoggerInstaller.Install(IWindsorContainer container, IConfigurationStore store) in C:\Sviluppo\IocWinFormTest\IocWinFormTest\IocWinFormTest\Installers\LoggerInstaller.cs:riga 18
in Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 324
in Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 674
in IocWinFormTest.Program.Main() in C:\Sviluppo\IocWinFormTest\IocWinFormTest\IocWinFormTest\Program.cs:riga 22
in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()

代码:

public class LoggerInstaller : IWindsorInstaller
{
    #region IWindsorInstaller Members

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithAppConfig());
    }

    #endregion
}

App.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <logger name="NHibernate">
      <level value="WARN" />
    </logger>
    <logger name="NHibernate.SQL">
      <level value="ALL" />
      <appender-ref ref="RollingFile" />
    </logger>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingFile" />
  <appender-ref ref="trace" />
</root>
<appender name="trace" type="log4net.Appender.TraceAppender">
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value=" %date %level %message%newline" />
  </layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="Public\Log" />
  <appendToFile value="true" />
  <datePattern value=".yyyyMMdd.\tx\t" />
  <rollingStyle value="Date" />
  <param name="StaticLogFileName" value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d %-8ndc %-5thread %-5level %logger %message %timestampms %n" />
  </layout>
</appender>
  </log4net>
</configuration>

我使用NuGet导入所有软件包,有趣的故事是我在网络应用程序中具有相同的配置并且它有效!

你能帮助我吗?

丹尼尔

2 个答案:

答案 0 :(得分:5)

您是否已将Castle.Services.Logging.Log4netIntegration.dll和所有其他必需的程序集复制到项目的文件夹中?

答案 1 :(得分:0)

将WinForm应用程序的目标从客户端配置文件更改为.Net Framework。 log4net依赖于System.Web作为支持程序集。如果您希望继续定位客户端个人资料,我的问题的答案会有更多详细信息。

Why does log4net 1.2.10 require System.Web?