I am using Log4Net's RollingLogFileAppender along with a custom layout class in my console application to print some metadata information in the header. I now want to package this custom layout class in a library and tell log4net to use it.
Previously I was successfully able to use the following appender config:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="DEBUG />
<file value="C:\Logs\MyConsoleApp.log"
type="log4net.Util.PatternString" />
<preserveLogFileNameExtension value="true" />
<datePattern value="_yyyyMMdd" />
<rollingStyle value="Date" />
<appendToFile value="true" />
<staticLogFileName value="false" />
<layout type="MyConsoleApp.MyLogLayout">
<conversionPattern value="%date{ISO8601}|%-5level|%message%newline" />
</layout>
</appender>
I then tried moving the MyLogLayout class into a library project "MyLibrary.MyLogLayout", imported it into MyConsoleApp, and tried updating my appender code as follows:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="DEBUG />
<file value="C:\Logs\MyConsoleApp.log"
type="log4net.Util.PatternString" />
<preserveLogFileNameExtension value="true" />
<datePattern value="_yyyyMMdd" />
<rollingStyle value="Date" />
<appendToFile value="true" />
<staticLogFileName value="false" />
<layout type="MyLibrary.MyLogLayout">
<conversionPattern value="%date{ISO8601}|%-5level|%message%newline" />
</layout>
</appender>
When trying to run it I get this error:
log4net:ERROR Failed to find type [MyLibrary.MyLogLayout]
System.TypeLoadException: Could not load type [MyLibrary.MyLogLayout]. Tried assembly [log4net, Version=1.2.15.0, Culture=neutral
, PublicKeyToken=669e0ddf0bb1aa2a] and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, Strin
g typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOn
Error, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(
XmlElement element, Type defaultTargetType, Type typeConstraint)
log4net:ERROR Failed to create object to set param: layout
I have double checked to make sure that the dll is in the same folder, and that the reference works as expected. What's weird is that if I create a class MyConsoleApp.MyLogLayoutChild
that just inherits everything from MyLibrary.MyLogLayout
and reference it everything works, however the main point of pushing this code into a library is so that I don't have to create a new class for every project I make, especially one that doesn't have any logic in it other than to inherit everything from a parent class.
Does log4net support loading custom types from a dll library or am I just doing something wrong?
It does not work because TypeLoader does not know where to find your class. You need to help it by specifying fully qualified name of type. Like