使用 Azure BlockBlob 进行日志记录(使用多个编写器方案写入文本文件)

Logging using Azure BlockBlob (Writing into text files using multiple writer scenarios)

我想使用 "classical" 日志方法将文本行写入文本文件。我不想将其保存在本地,而是想将数据写入 blockblob。这是代码:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer share = blobClient.GetContainerReference("logStorage");
var logFile=share.GetAppendBlobReference("mylog.log");
logFile.AppendText("This is a log entry");

这有效,但仅适用于单作者场景,因为文档明确说明了 AppendText():

This API should be used strictly in a single writer scenario because the API internally uses the append-offset conditional header to avoid duplicate blocks which does not work in a multiple writer scenario.

因此,如果我在 多个编写器 场景中需要该功能,我必须使用什么作为替代方案?

对于多写者场景,调用客户端库中的Append Block instead of AppendText. See the AppendBlock方法。

追加块提供原子性,但将有效负载限制为单个块的大小。

@OleAlbers,正如@TamraMyers-Microsoft 所说,下面来自 REST API Append Block 部分的描述 "Avoiding duplicate or delayed appends",它是多编写器场景的一种方式。

In a multiple writer scenario, each client can use conditional headers, but this may not be an optimal approach in terms of performance.

但参考文献也如下所述

For the highest concurrent append throughput, applications should handle redundant appends and delayed appends in their application layer (e.g., add epochs or sequence numbers in the data being appended).

我认为满足您需求的最简单且最好的方法是使用无锁解决方案,例如新的 Dataflow library, part of the Async CTP,以异步执行多写入器操作。

您可以尝试参考教程How to: Write Messages to and Read Messages from a Dataflow Block改写您的单写代码以适应多写场景。