0

I'm using log4net with an memoryappender. When I try to read all the lines to an variable (here: StringBuilder) I'm getting an OutOfMemory-Exception when the amount of lines is to high. I've tested it with 1mio lines:

public class RenderingMemoryAppender : MemoryAppender
{
    public IEnumerable<string> GetRenderedEvents(List<Level> levelList = null)
    {
        foreach (var loggingEvent in GetEvents())
        {
            yield return RenderLoggingEvent(loggingEvent);
        }
    }

    public byte[] GetEventsAsByteArray(List<Level> levelList=null )
    {
        var events = GetRenderedEvents(levelList);
        var s = new StringBuilder();
        foreach (var e in events)
        {
            s.Append(e);
        }
        //the exception is thrown here below when calling s.ToString().
        return Encoding.UTF8.GetBytes(s.ToString());
    }
}

when I simply add one-million lines to an stringbuilder without the log4net component everything works fine...

I've also tried to use this:

var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder);

foreach (var loggingEvent in GetEvents())
{
    stringBuilder.Clear();
    loggingEvent.WriteRenderedMessage(stringWriter);

    list.Add(stringBuilder.ToString());
}

but this also didn't work.

Tobias Koller
  • 2,116
  • 4
  • 26
  • 46
  • 1
    Possible duplicate of [StringBuilder.ToString() throws OutOfMemory Exception](http://stackoverflow.com/questions/25010604/stringbuilder-tostring-throws-outofmemory-exception) – raven Jan 05 '17 at 12:57

1 Answers1

0

If you want to have that many lines in memory the runtime wants to allocate a piece of memory that can contain the whole string. If it is not possible because at that moment, the OutOfMemory exception is thrown. If you need the bytes from a memorystream it is more efficient to call the ToArray() method on the memorystream, that can also fail for the same reason as the ToString method fails on the StringBuilder. You can check if you run in 64bit mode, if not, it can help to get more address space. I would advice to rethink your logging method. The way you are doing it now is unreliable and can even break your program.

Peter
  • 27,590
  • 8
  • 64
  • 84