当文件达到5.4MB时,自定义log4net azure blob附加程序停止记录

时间:2018-09-27 12:41:58

标签: c# log4net azure-blob-storage

我正在使用下面的附加程序通过Log4Net将日志消息通过管道传输到Azure。 一切正常,直到blob文件达到5.4MB的大小为止。从此以后,不再将日志追加到日志文件。

是什么原因造成的?我该如何解决呢?

namespace Digicreate.Core.Infrastructure.Logging
{
using System;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using log4net.Appender;
using log4net.Config;
using log4net.Core;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

// ReSharper disable once UnusedMember.Global
// justification: used in log4net configuration file
public class AzureBlobTxtFileAppender : BufferingAppenderSkeleton
{
    private CloudStorageAccount _account;
  ...removed unimportant code...

            protected override void SendBuffer(LoggingEvent[] events)
    {
        var appendBlobReference = _cloudBlobContainer.GetAppendBlobReference(Filename(DirectoryName));
        if (!appendBlobReference.Exists())
        {
            appendBlobReference.CreateOrReplace();
        }

        Parallel.ForEach(events, ProcessEvent);
    }

    private static string Filename(string directoryName)
    {
        return $"{directoryName}/{DateTime.Today.ToString("yyyy_MM_dd", DateTimeFormatInfo.InvariantInfo)}.log.txt";
    }

    private void ProcessEvent(LoggingEvent loggingEvent)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
            {
                Layout.Format(streamWriter, loggingEvent);
                streamWriter.Flush();
            }

            memoryStream.Position = 0;

            _cloudBlobContainer
                .GetAppendBlobReference(Filename(DirectoryName))
                .AppendBlock(memoryStream);
        }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

根据附加块documentation,一个Blob可能达到50,000个块限制。在使用以下方法提交到Blob之前,请确保检查提交的块数:

image

并确保不超过50000。