将动态值传递给appender
我正在创建不同的报告并尝试传递报告名称并将其附加到 log4net 报告的名称中。每次运行报告时,报告名称都会不同,因此将动态分配。
我尝试使用 LOG4NET.GLOBALCONTEXT.PROPERTIES("RptName") = sReportName"
,但我得到的只是报告标题中的“null”值。
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log4net.GlobalContext.**Properties("RptName")** = sReportName
log.Info("this is an info message");
Console.ReadLine();
}
}
<!--RollingFileAppender: Provides output of all log entries to a file in the \logs directory
Filename: RSGetReport_yyyyMMdd_hh:mm:ss_processID.txt-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="logs\RSGetReport_%date{yyyyMMdd}_%date{hhmmss}_%processid**_%property{myContext}.**txt" />
<appendToFile value="true" />
<rollingStyle value="Once" />
<maxSizeRollBackups value="10" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ALL" />
</filter>
<maximumFileSize value="1MB" />
<countDirection value="1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>strong text
I am creating different reports and trying to pass the report name and append it to the name of the log4net report. The report name will be different each time the report is run so it will be dynamically assigned.
I've tried using the LOG4NET.GLOBALCONTEXT.PROPERTIES("RptName") = sReportName"
, but all I get is a 'null' value in the report title.
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log4net.GlobalContext.**Properties("RptName")** = sReportName
log.Info("this is an info message");
Console.ReadLine();
}
}
<!--RollingFileAppender: Provides output of all log entries to a file in the \logs directory
Filename: RSGetReport_yyyyMMdd_hh:mm:ss_processID.txt-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="logs\RSGetReport_%date{yyyyMMdd}_%date{hhmmss}_%processid**_%property{myContext}.**txt" />
<appendToFile value="true" />
<rollingStyle value="Once" />
<maxSizeRollBackups value="10" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ALL" />
</filter>
<maximumFileSize value="1MB" />
<countDirection value="1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>strong text
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在配置log4net之前,您需要设置全局属性。另请参阅此问题。
You need to set the global property before configuring log4net. See also this question.