I am using log4net within a Console Application. My configuration file allow me to get the filename from Input:
Config file:
<appender name="CustomRollingFileAppender" type="MyApp.UTIL.CustomRollingFileAppender">
<threshold value="ALL"/>
<param name="file" value=""/>
<param name="appendToFile" value="false"/>
<param name="maximumFileSize" value="20000KB"/>
<param name="maxSizeRollBackups" value="200"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
</layout>
</appender>
Override File Parameter:
namespace ConsoleApplicationTemplate.UTIL
{
public class CustomRollingFileAppender : log4net.Appender.RollingFileAppender
{
public override string File
{
get { return base.File; }
set
{
//Filename string taken as Input
base.File = Program.options.LogFile;
}
}
}
}
This is working like a charm, but I need to change the parameter type inside the tag appender. I have searched around and find out that I can use properties within configuration file. So I have modified my config file as following:
<appender name="CustomRollingFileAppender" type="%property{ApplicationName}.UTIL.CustomRollingFileAppender">
<threshold value="ALL"/>
<param name="file" value=""/>
<param name="appendToFile" value="false"/>
<param name="maximumFileSize" value="20000KB"/>
<param name="maxSizeRollBackups" value="200"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5.5level] %logger - %message%newline"/>
</layout>
</appender>
And I also need to change the property value as following:
log4net.GlobalContext.Properties["ApplicationName"] = Assembly.GetExecutingAssembly().GetName().Name;
private static log4net.ILog _logger = LogManager.GetLogger(typeof(Program));
log4net.Config.XmlConfigurator.Configure();
When log4net read the configuration file I get the following error (which doesn’t raise an exception):
log4net:ERROR Could not create Appender [CustomRollingFileAppender] of type [%property{ApplicationName}.UTIL.CustomRollingFileAppender]. Reported error follows.
System.TypeLoadException: Could not load type [%property{ApplicationName}.UTIL.CustomRollingFileAppender]. 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(XmlEle
ment appenderElement)
I’m afraid it appears that the answer is no: Unlike some of the other log4net config elements, the appender element’s type attribute does not support parameters.
As discussed in the comments to your question, since what you are trying to achieve is making a Visual Studio project template that provides its own log4net appender type, a solution would be to make use of the Visual Studio templating system to write the relevant attribute value in the .config file.