以编程方式添加 log4net appender,但布局不起作用
Add log4net appender programmatically, but layout does not work
我有以下代码将滚动文件附加程序添加到 log4net。
ILog myLogger = LogManager.GetLogger("Audit");
var auditAppender = new RollingFileAppender()
{
Name = "AuditAppender",
File = this.fullyQualifiedFileName,
DatePattern = "yyyyMMdd",
RollingStyle = RollingFileAppender.RollingMode.Date,
AppendToFile = true,
Layout = new PatternLayout()
{
ConversionPattern = "%newline %date %-5level %newline%message%newline",
},
};
auditAppender.ActivateOptions();
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)myLogger.Logger;
l.AddAppender(auditAppender);
l.Repository.Configured = true;
myLogger.Info(message);
但是结果好像完全不符合我的ConversionPattern
。有什么线索吗?
你们非常非常亲密 ;-)
您唯一错过的是您还需要致电 PatternLayout.ActivateOptions
:
var auditAppender = new RollingFileAppender()
{
// snip
}
((PatternLayout)auditAppender.Layout).ActivateOptions();
auditAppender.ActivateOptions();
我有以下代码将滚动文件附加程序添加到 log4net。
ILog myLogger = LogManager.GetLogger("Audit");
var auditAppender = new RollingFileAppender()
{
Name = "AuditAppender",
File = this.fullyQualifiedFileName,
DatePattern = "yyyyMMdd",
RollingStyle = RollingFileAppender.RollingMode.Date,
AppendToFile = true,
Layout = new PatternLayout()
{
ConversionPattern = "%newline %date %-5level %newline%message%newline",
},
};
auditAppender.ActivateOptions();
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)myLogger.Logger;
l.AddAppender(auditAppender);
l.Repository.Configured = true;
myLogger.Info(message);
但是结果好像完全不符合我的ConversionPattern
。有什么线索吗?
你们非常非常亲密 ;-)
您唯一错过的是您还需要致电 PatternLayout.ActivateOptions
:
var auditAppender = new RollingFileAppender()
{
// snip
}
((PatternLayout)auditAppender.Layout).ActivateOptions();
auditAppender.ActivateOptions();