如何将csv日志存储到Azure AppendBlob而不影响任何性能?
将csv写入内存流的代码:
public static byte[] WriteCsvWithHeaderToMemory<T>(IEnumerable<T> records) where T : class
{
var outputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
EnforceCsvColumnAttribute = true,
};
using (var memoryStream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(memoryStream))
{
var context = new CsvContext();
context.Write(records, streamWriter, outputFileDescription);
}
return memoryStream.ToArray();
}
}
由于
答案 0 :(得分:0)
public async Task WriteToAudit<T>(T logData)
{
var list = new List<T> {logData};
var appendBlob = GetAppendBlobReference();
appendBlob.FetchAttributes();
var auditDataBytes = WriteCsvWithHeaderToMemory(list,appendBlob.Properties.Length);
using (var stream = new MemoryStream(auditDataBytes))
{
await appendBlob.AppendBlockAsync(stream).ConfigureAwait(false);
}
}
public static byte[] WriteCsvWithHeaderToMemory<T>(IEnumerable<T> records, long blobSize) where T : class
{
var outputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
EnforceCsvColumnAttribute = true,
FirstLineHasColumnNames = blobSize <= 0
};
using (var memoryStream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(memoryStream))
{
var context = new CsvContext();
context.Write(records, streamWriter, outputFileDescription);
}
return memoryStream.ToArray();
}
}