我在控制台应用程序中使用log4net。我的配置文件允许我从Input:
获取文件名配置文件:
<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>
覆盖文件参数:
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;
}
}
}
}
这就像魅力一样,但我需要在标签 appender 中更改参数类型。我搜索过并发现我可以在配置文件中使用属性。所以我修改了我的配置文件如下:
<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>
我还需要更改属性值,如下所示:
log4net.GlobalContext.Properties["ApplicationName"] = Assembly.GetExecutingAssembly().GetName().Name;
private static log4net.ILog _logger = LogManager.GetLogger(typeof(Program));
log4net.Config.XmlConfigurator.Configure();
当log4net读取配置文件时,我收到以下错误(不会引发异常):
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)
答案 0 :(得分:1)
我担心似乎答案是否定的:与其他一些log4net配置元素不同,appender元素的type属性不支持参数。
正如您对问题的评论中所讨论的,因为您要实现的是创建一个提供自己的log4net appender类型的Visual Studio项目模板,解决方案是使用Visual Studio模板系统来编写.config文件中的相关属性值。