Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryAppender PatternLayout not rendering?

Tags:

c#

log4net

App.config looks like this:

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

Code looks like this:

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

The string returned from ev.RenderedMessage is just the message, not the entire pattern. I've also tried using WriteRenderedMessage with a StringWriter and the result was the same. Is there a way to get the message with the pattern applied?

like image 729
Antony Woods Avatar asked Jan 21 '13 12:01

Antony Woods


1 Answers

I found it :)

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();
}
like image 140
Antony Woods Avatar answered Nov 09 '22 18:11

Antony Woods