Search code examples
c#azureloggingazure-blob-storagetrace

Upload Trace Log Files Directly to Azure


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?


Solution

  • 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

    1. 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");
            }
    
    1. 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();
            }
    

    enter image description here enter image description here