Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly not loaded c#

Tags:

c#

.net

log4net

I have a project Common which contains a log4net CustomAppender. I reference the project in all my other projects and configure log4net appender in app.config. Everything works smooth except for one project which fails when trying to instantiate the Appender.

The output shows the following error:

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.

The log4net configuration is the same for all project. app.config contains:

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

From code I call log4net.Config.XmlConfigurator.Configure(). If I manually load the assembly using Assembly.Load("Common") before calling log4net configure for the non-working project everything is fine.

Any idea why the assembly is not loaded when MyCustomAppender is instantiated via reflection? How can I solve this?

like image 647
Dan Dinu Avatar asked Nov 15 '25 06:11

Dan Dinu


1 Answers

Try putting the name of the assembly after the name of the type

<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

Keep in mind that the name of an assembly is not necessarily the same as it's default namespace nor the project name in Visual Studio. The relevant name here is the output name (which is the file name of the resulting dll)

like image 177
Rick Hodder Avatar answered Nov 17 '25 22:11

Rick Hodder