MemoryAppender PatternLayout无法渲染?

时间:2013-01-21 12:04:59

标签: c# log4net

App.config如下所示:

<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level - %message" />
  </layout>
</appender>

代码如下所示:

foreach (var ev in events)
{
   string msg = ev.RenderedMessage;
}

ev.RenderedMessage返回的字符串只是消息,而不是整个模式。我也尝试将WriteRenderedMessageStringWriter一起使用,结果是一样的。有没有办法获得应用模式的消息?

1 个答案:

答案 0 :(得分:17)

我找到了它:)

var events = _ma.GetEvents();
string result = "";
using (StringWriter writer = new StringWriter())
{
    foreach (var ev in events)
    {
        _ma.Layout.Format(writer, ev);
        writer.Write(Environment.NewLine);
    }
    result = writer.ToString();
}