c# – MemoryAppender PatternLayout不呈现?

前端之家收集整理的这篇文章主要介绍了c# – MemoryAppender PatternLayout不呈现?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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返回的字符串只是消息,而不是整个模式.我也尝试过使用WriteRenderedMessage和StringWriter,结果是一样的.有没有办法获取应用模式的消息?

解决方法

我找到了 :)
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();
}
原文链接:/csharp/93112.html

猜你在找的C#相关文章