我正在使用log4net.Appender.Azure中的AzureAppendBlobAppender写入blob。 但是日志以XML格式生成。如何以RollingFileAppender生成的方式在普通文本文件中获取它们。
<appender name="AzureAppender2" type="log4net.Appender.AzureAppendBlobAppender, log4net.Appender.Azure">
<param name="ContainerName" value="#######" />
<param name="DirectoryName" value="######" />
<param name="ConnectionString" value="#####"/>
<datePattern value="'.'yyyyMMdd'.log'" />
<maximumFileSize value="5MB" />
<bufferSize value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger %machine - %message e:%exception%newline %newline" />
</layout>
</appender>
答案 0 :(得分:0)
全部
由于log4net.Appender.Azure是开源的,因此我可以对其进行自定义。 我通过修改AzureAppendBlobAppender.cs中的以下方法找到了解决方案。
private void ProcessEvent(LoggingEvent loggingEvent)
{
CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(_directoryName));
var log = "";
if (loggingEvent.Level == Level.Error)
{
log = Environment.NewLine + string.Format("{0} Exception : " + Environment.NewLine + "{1}", loggingEvent.TimeStamp, loggingEvent.GetExceptionString());
}
else
log = Environment.NewLine + string.Format("{0} Information : " + Environment.NewLine + "{1}", loggingEvent.TimeStamp, loggingEvent.RenderedMessage);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(log)))
{
appendBlob.AppendBlock(ms);
}
}