0

I joined Azure today to deploy my ASP.Net web site that is now on GoDaddy, need to be able the scale. The first problem was a database connection issue but that log error failed because the file system is different. I can write to the web site file system in my development and on GoDaddy but Azure requires files to be on a separate storage system.

Reworking my file write code with an "if Azure" condition I got the following code from different Stack Overflow answers.

using Microsoft.WindowsAzure.Storage.File;
using System.IO;
using System.Text;
namespace SignupList.Com.SignupList.Data
{
    public class AzureFileStorage
    {
        private CloudFileClient fileClient = null;

        public AzureFileStorage()
        {
        }

        private void Initalize()
        {
            if (fileClient == null) {
                CloudFileClient fileClient = SignUpPlace.AzureStoreAccount.CreateCloudFileClient();
            }
        }

        CloudFileShare share = null;
        CloudFileDirectory directoryDir = null;
        private CloudFileDirectory GetDirectory(string shareName, string directoryName, string filename)
        {
            Initalize();

            if (share == null || (share != null && share.Name != shareName)) {
                share = fileClient.GetShareReference(shareName);
                if (!share.Exists()) {
                    share.Create();
                }
            }


            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            if (Helper.IsEmpty(directoryName)) {
                directoryDir = rootDir;
            }
            else {
                directoryDir = rootDir.GetDirectoryReference(directoryName);
                if (!directoryDir.Exists()) {
                    directoryDir.Create();
                }
            }

            return directoryDir;
        }

        public void WriteLine(string shareName, string directoryName, string filename, string text)
        {
            CloudFileDirectory directoryRef = GetDirectory(shareName, directoryName, filename);

            CloudFile fileRef = directoryRef.GetFileReference(filename);
            if (!fileRef.Exists()) {
                fileRef.Create(0);
            }

            byte[] buffer = Encoding.UTF8.GetBytes($"{text}\r\n");

            fileRef.Resize(fileRef.Properties.Length + buffer.Length);

            using (CloudFileStream fileStream = fileRef.OpenWrite(null)) {
                fileStream.Seek(buffer.Length * -1, SeekOrigin.End);
                fileStream.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

The append code came from Append using CloudFile which had a couple corrective comments. Sadly, the comments did not provide any code corrections, I did not really understand what they wanted over the given solution.

Unfortunately, I cannot test this on my local development not having shares, right? It compiles. Does it look alright?

When I Google for "Azure CloudFile append" it always finds CloudBlob answers, especially concerning CloudAppendBlob.

I cannot find anything explaining the difference between a CloudFile and CloudBlob file. Would a CloubBlob work fine for me appending log debug lines in a log.txt file? Could I stream a CloubBlob fine onto a Show Log webpage? Can I open a CloudBlob created file using CloudFile interchangeably?

Another thing that came up, several examples used Console.WriteLine(text). I found that Console is a file in AppData. Is that still true in Azure? In Azure, can I get the Azure website to display the console file with its log system?

At least I was expecting such problems when deploying to Azure.

George
  • 307
  • 1
  • 5
  • 12
  • 1
    It sounds like [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why does database connection issue lead you to Azure Storage? If it is about logging, you might want to consider looking into [Application Insights](https://azure.microsoft.com/en-us/services/application-insights/). – Win Feb 23 '18 at 01:28
  • 1
    First: there is quite a bit of documentation around Azure Blobs and Azure Files. Second: You can absolutely write to a local file system. Why do you think otherwise? Virtual Machines have Durable Storage for their os disk, a built-in temporary disk with more (volatile) storage, and ability to attach more disks (durable as well) to your VM. Azure Web Apps have built-in durable storage accessible by all instances. So... I don't understand your issue or question. And you also haven't shared how you deployed (Virtual Machine? Web App?). Please edit your question accordingly. – David Makogon Feb 23 '18 at 04:16
  • The database connect throws an exception which is caught and logged to the error log. The error log trying the write to a file it creates in the website then throws an exception which is displayed as a web page. My current problem is getting back to a functioning error log system on Azure, which this question is about. Perhaps I should have been using Console.WriteLine() for my error log instead of my own file append creation, also part of the question. – George Feb 23 '18 at 17:07

0 Answers0