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.