装配未加载c#

我有一个项目 Common ,其中包含一个log4net CustomAppender . 我在所有其他项目中引用该项目,并在 app.config 中配置log4net appender . 除了在尝试实例化Appender时失败的一个项目,一切都顺利进行 .

输出显示以下错误:

System.TypeLoadException: Could not load type [Common.Appenders.MyCustomAppender].
Tried assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]
and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [MyCustomAppender] not found.

所有项目的log4net配置都相同 . app.config 包含:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="MyCustomAppender" type="Common.Appenders.MyCustomAppender">
      <file value="log.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="MyCustomAppender" />
    </root>
  </log4net>
</configuration>

从代码我调用 log4net.Config.XmlConfigurator.Configure() . 如果我在调用log4net之前使用 Assembly.Load("Common") 手动加载程序集为非工作项目配置一切都很好 .

MyCustomAppender 通过反射实例化时,知道为什么没有加载程序集?我怎么解决这个问题?

回答(1)

2 years ago

尝试将类型的名称放在类型的名称后面

<appender name="MyCustomAppender" type="Common.Appenders.MyCustomAppender,Common">

http://weblogs.asp.net/tgraham/archive/2007/05/02/loading-the-assembly-for-a-custom-log4net-appender.aspx