11

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只是消息,而不是整个模式。我也试过用WriteRenderedMessagea StringWriter,结果是一样的。有没有办法在应用了模式的情况下获取消息?

4

1 回答 1

18

我找到了 :)

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();
}
于 2013-01-21T12:09:06.950 回答