将跟踪日志文件直接上载到Azure [英] Upload Trace Log Files Directly to Azure
问题描述
我正在使用System Diagnostics跟踪编写来在我的应用程序中进行日志记录.我想将日志文件上传到Azure存储.我能够这样做,但是只能通过上传存储在我的项目文件夹中的日志来做到.我创建了一个自定义的跟踪侦听器来定向文件的上传位置.
公共TextLogTraceListener(字符串filePath,字符串db){filePath = filePath + db +"\\" + DateTime.Now.ToString("MMddyyyyy")+"_mylog.log";logFileLocation = filePath;traceWriter =新的StreamWriter(filePath,true);traceWriter.AutoFlush = true;}
在一个单独的功能中,我使用以下代码将存储在我的项目文件夹中的日志文件上传到Azure存储中
使用(var fileStream = File.OpenRead(path)){blockBlob.UploadFromStream(fileStream);}
但是,我想削减中间人并将日志直接上传到Azure存储.我该怎么办?
如果要使用Azure blob存储作为日志文件系统,我们可以使用
I'm using System Diagnostics trace writing to do logging in my application. I want to upload my log files to Azure Storage. I'm able to do so but only by uploading logs that are stored in my project folder. I created a custom Trace Listener to direct where the file is uploaded.
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;
}
In a separate function I'm using the following code to upload the log file stored in my project folder to the Azure storage
using (var fileStream = File.OpenRead(path))
{
blockBlob.UploadFromStream(fileStream);
}
However, I want to cut out the middle-man and upload the logs directly to the Azure storage. How do I go about that?
If you want to use Azure blob storage as your log file system, we can use Azure append blob to store log file.
For example
- Create a custom Trace Listener
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");
}
- Test
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!