3
votes

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?

1
Is the Common project marked as Copy Local for the build? - Rick Hodder
@RickHodder Yes, it is and all the required dlls are in place. - Dan Dinu
There is nothing in the .config file that could help log4net figure out that it needs to load an assembly itself with the name "Common". The namespace name of the type is not relevant. So it doesn't work. Try appending , Common like it was done in the "type" attribute above it. I personally got quickly fed up with an xml programming language without a debugger and just configure it in code. - Hans Passant

1 Answers

13
votes

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)