log4net %属性 不工作
log4net %property does not work
我有一个小型测试项目,代码如下:
class Program
{
static void Main(string[] args)
{
log4net.GlobalContext.Properties["logFileName"] = "log.txt";
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.xml"));
log4net.ILog logger = log4net.LogManager.GetLogger("Tests");
logger.Debug("Test message");
}
}
我的log4net.xml配置文件有以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="%property{logFileName}" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{ISO8601} [%level][%logger] %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
我的问题是为什么 log4net 创建名称为 %属性{logFileName} 的文件并且不将其替换为 log.txt?
log4net 库取自 NuGet (Id:log4net Version:2.0.3).
请在您的 xml 配置中使用它
<file type="log4net.Util.PatternString" value=".\%property{logFileName}" />
我有一个小型测试项目,代码如下:
class Program
{
static void Main(string[] args)
{
log4net.GlobalContext.Properties["logFileName"] = "log.txt";
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.xml"));
log4net.ILog logger = log4net.LogManager.GetLogger("Tests");
logger.Debug("Test message");
}
}
我的log4net.xml配置文件有以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="%property{logFileName}" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{ISO8601} [%level][%logger] %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
我的问题是为什么 log4net 创建名称为 %属性{logFileName} 的文件并且不将其替换为 log.txt?
log4net 库取自 NuGet (Id:log4net Version:2.0.3).
请在您的 xml 配置中使用它
<file type="log4net.Util.PatternString" value=".\%property{logFileName}" />