将跟踪日志文件直接上传到 Azure
Upload Trace Log Files Directly to Azure
我正在使用系统诊断跟踪写入来记录我的应用程序。我想将我的日志文件上传到 Azure 存储。我能够这样做,但只能通过上传存储在我的项目文件夹中的日志来实现。我创建了一个自定义跟踪侦听器来指示文件上传的位置。
public TextLogTraceListener(string filePath, string db)
{
filePath = filePath + db + "\" + DateTime.Now.ToString("MMddyyyy") + "_mylog.log";
logFileLocation = filePath;
traceWriter = new StreamWriter(filePath, true);
traceWriter.AutoFlush = true;
}
在一个单独的函数中,我使用以下代码将存储在我的项目文件夹中的日志文件上传到 Azure 存储
using (var fileStream = File.OpenRead(path))
{
blockBlob.UploadFromStream(fileStream);
}
不过,我想去掉中间人,直接将日志上传到Azure存储。我该怎么做?
如果您想使用 Azure blob 存储作为您的日志文件系统,我们可以使用 Azure append blob 来存储日志文件。
例如
- 创建自定义跟踪侦听器
public class BlobWriterStorageListener : TraceListener
{
// Install package Microsoft.Azure.Storage.Blob
protected override string[] GetSupportedAttributes()
{
return new[] { "StorageConnectionString", "LogsContainerName", "LogFileName" };
}
public override void Write(string message, string category) {
string stroageConnectionString = Attributes["StorageConnectionString"];
string logsContainerName = Attributes["LogsContainerName"];
string logFileName = Attributes["LogFileName"];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(stroageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(logsContainerName);
container.CreateIfNotExists();
CloudAppendBlob appendBlob = container.GetAppendBlobReference(logFileName);
// when the blob does not exist, create it.
if (!appendBlob.Exists()) {
appendBlob.CreateOrReplace();
}
appendBlob.AppendText(String.Format("[Timestamp: {2:u}] Message:{0} Category:{1}", message, category, DateTime.UtcNow));
}
public override void WriteLine(string message, string category)
{
Write(message, category +"\n");
}
public override void Write(string message)
{
Write(message, null);
}
public override void WriteLine(string message)
{
Write(message + "\n");
}
- 测试
static void Main(string[] args)
{
TraceListener ooblistener = new BlobWriterStorageListener();
ooblistener.Name = "AzureBlobStorageListener";
ooblistener.Attributes.Add("type", "BlobTrace.BlobWriterStorageListener");
ooblistener.Attributes.Add("StorageConnectionString", "<your storage connection string>");
ooblistener.Attributes.Add("LogsContainerName", "logs");
ooblistener.Attributes.Add("LogFileName", "application.log");
Trace.Listeners.Add(ooblistener);
Trace.WriteLine("Hey There!!", EventLogEntryType.Information.ToString());
Thread.Sleep(60000);
Trace.WriteLine("Hey Here!!", EventLogEntryType.Information.ToString());
Console.ReadLine();
}
我正在使用系统诊断跟踪写入来记录我的应用程序。我想将我的日志文件上传到 Azure 存储。我能够这样做,但只能通过上传存储在我的项目文件夹中的日志来实现。我创建了一个自定义跟踪侦听器来指示文件上传的位置。
public TextLogTraceListener(string filePath, string db)
{
filePath = filePath + db + "\" + DateTime.Now.ToString("MMddyyyy") + "_mylog.log";
logFileLocation = filePath;
traceWriter = new StreamWriter(filePath, true);
traceWriter.AutoFlush = true;
}
在一个单独的函数中,我使用以下代码将存储在我的项目文件夹中的日志文件上传到 Azure 存储
using (var fileStream = File.OpenRead(path))
{
blockBlob.UploadFromStream(fileStream);
}
不过,我想去掉中间人,直接将日志上传到Azure存储。我该怎么做?
如果您想使用 Azure blob 存储作为您的日志文件系统,我们可以使用 Azure append blob 来存储日志文件。
例如
- 创建自定义跟踪侦听器
public class BlobWriterStorageListener : TraceListener
{
// Install package Microsoft.Azure.Storage.Blob
protected override string[] GetSupportedAttributes()
{
return new[] { "StorageConnectionString", "LogsContainerName", "LogFileName" };
}
public override void Write(string message, string category) {
string stroageConnectionString = Attributes["StorageConnectionString"];
string logsContainerName = Attributes["LogsContainerName"];
string logFileName = Attributes["LogFileName"];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(stroageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(logsContainerName);
container.CreateIfNotExists();
CloudAppendBlob appendBlob = container.GetAppendBlobReference(logFileName);
// when the blob does not exist, create it.
if (!appendBlob.Exists()) {
appendBlob.CreateOrReplace();
}
appendBlob.AppendText(String.Format("[Timestamp: {2:u}] Message:{0} Category:{1}", message, category, DateTime.UtcNow));
}
public override void WriteLine(string message, string category)
{
Write(message, category +"\n");
}
public override void Write(string message)
{
Write(message, null);
}
public override void WriteLine(string message)
{
Write(message + "\n");
}
- 测试
static void Main(string[] args)
{
TraceListener ooblistener = new BlobWriterStorageListener();
ooblistener.Name = "AzureBlobStorageListener";
ooblistener.Attributes.Add("type", "BlobTrace.BlobWriterStorageListener");
ooblistener.Attributes.Add("StorageConnectionString", "<your storage connection string>");
ooblistener.Attributes.Add("LogsContainerName", "logs");
ooblistener.Attributes.Add("LogFileName", "application.log");
Trace.Listeners.Add(ooblistener);
Trace.WriteLine("Hey There!!", EventLogEntryType.Information.ToString());
Thread.Sleep(60000);
Trace.WriteLine("Hey Here!!", EventLogEntryType.Information.ToString());
Console.ReadLine();
}