我通过从 AppenderSkeleton 继承实现了一个自定义附加程序。我的记录器实例已正确创建,并且在记录时调用了“附加”方法,但消息格式不正确。我通过传递 StreamWriter 实例尝试了几种不同的方法,但是对它的所有调用都无法写入流。我尝试过的另一件事是使用 RenderedMessage 属性,但返回的值是没有布局格式的确切消息。
protected override void Append(log4net.Core.LoggingEvent loggingEvent)
{
StreamWriter _writer = new StreamWriter(new MemoryStream());
RenderLoggingEvent(_writer, loggingEvent);
//After writing the logging event the base stream position remains 0
byte[] messageBuffer = new byte[_writer.BaseStream.Position];
_writer.BaseStream.Read(messageBuffer, 0, messageBuffer.Length);
//Also trying to read from a stream reader created from the base stream fails
var reader = new StreamReader(_writer.BaseStream);
var test = reader.ReadToEnd();
//variable test is not formatted correctly according to the conversion pattern
test = loggingEvent.RenderedMessage;
}
<appender name="TestAppender" type="log4netTestApp">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date]-[%thread]-[%level]-[%logger] - %message%newline" />
</layout>
</appender>
关于什么是错的任何想法?
从内存流更改为文件流具有正确的布局,因此它必须与使用内存流有关。