【问题标题】:Upload Trace Log Files Directly to Azure将跟踪日志文件直接上传到 Azure
【发布时间】:2020-09-27 13:02:57
【问题描述】:

我正在使用系统诊断跟踪写入来登录我的应用程序。我想将我的日志文件上传到 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 存储。我该怎么办?

【问题讨论】:

    标签: c# azure logging azure-blob-storage trace


    【解决方案1】:

    如果您想使用 Azure blob 存储作为您的日志文件系统,我们可以使用Azure append blob 来存储日志文件。

    例如

    1. 创建自定义跟踪侦听器
    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");
            }
    
    1. 测试
     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();
            }
    

    【讨论】:

      猜你喜欢
      • 2011-11-02
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2012-07-02
      • 2010-12-11
      相关资源
      最近更新 更多