Azure provides Pay as you go service, so it is important to use the services efficiently. Pricing of table storage is higher than blob storage, so it is good idea to archive your logs to the blob storage. Diagnostic enabled for Azure cloud service configuration.
We and retrieve record from both the tables ( WadLogsTable, WADWindowsEventLogTable).
/// <summary>
/// Retrives Log data from Azure storage table
/// </summary>
/// <typeparam name="T">Table type for which we will retrive record</typeparam>
/// <param name="environment">Environment (UAT,PROD). </param>
/// <param name="startTick">starting/ from tick</param>
/// <param name="endTick">ending/ to Tick</param>
/// <returns></returns>
private List<T> GetLogFromTable<T>(string environment, long startTick, long endTick)
where T : TableEntity, new()
{
//Initializing storage Account with the proper storage connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[environment].ToString());
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
//Get the referance of the storage table
CloudTable table = tableClient.GetTableReference(typeof(T).Name);
//Checks If table exists
if (table.Exists())
{
//Partition key in the log table are date time tick
//Creating Query Partition key between start and end ticks
TableQuery<T> q = new TableQuery<T>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.GreaterThanOrEqual, "0" + startTick),
TableOperators.And,
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThan, "0" + end Tick)));
//Retriving Records from storage table
return table.ExecuteQuery<T>(q).ToList<T>();
}
return null;
}
/// <summary>
/// Appends the data in text file and convert it to bytes
/// </summary>
/// <typeparam name="T">Table type for which is retrive record</typeparam>
/// <param name="list">Log data retrived from table storage</param>
/// <param name="fileName">name of the file</param>
/// <returns>bytes of file</returns>
private byte[] ConvertTableToBytes<T>( IList<T> list, string fileName)
where T : TableEntity, new()
{
// Instances which will help in creating the content of file
StringBuilder headerText = new StringBuilder();
StringBuilder contentText = new StringBuilder();
List<PropertyInfo> listOfProperties = new List<PropertyInfo>();
string delemiter = "\t";
string filetype = "txt";
//Retriving the list of property
foreach (var prop in typeof(T).GetProperties())
{
listOfProperties.Add(prop);
headerText.AppendFormat("{0}{1}", prop.Name, delemiter);
}
StringBuilder logText = new StringBuilder();
//Appending header text
logText.AppendLine(headerText.ToString());
//Appending data from each item
foreach (T item in list)
{
contentText.Clear();
foreach (var prop in listOfProperties)
{
contentText.AppendFormat("{0}{1}", prop.GetValue(item), delemiter);
}
logText.AppendLine(contentText.ToString());
}
//Storing file in temp location and converting it in bytes
File.WriteAllText("_" + fileName + "." + filetype, logText.ToString(), Encoding.Unicode);
byte[] result = File.ReadAllBytes("_" + fileName + "." + filetype);
return result;
}
/// <summary>
/// Upload the bytes of data to blob storage
/// </summary>
/// <param name="environment">Environment (UAT,PROD). </param>
/// <param name="Container">Name of container in which files will be archive</param>
/// <param name="fileName">Name of file</param>
/// <param name="file">Data of file in byte format</param>
/// <returns></returns>
private string UploadToBlob(string environment, string Container, string fileName, byte[] file)
{
//Initializing storage Account with the proper storage connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[environment].ToString());
// Create the blob client.
CloudBlobClient blogClient = storageAccount.CreateCloudBlobClient();
//Referencing Container
CloudBlobContainer container = blogClient.GetContainerReference(Container.ToLower());
//If container not exist it will create a new container with specified name
container.CreateIfNotExists();
//referance file name inside container
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
//Create a stream and uploading the data in blob storage
using (var memoryStream = new System.IO.MemoryStream(file))
{
blockBlob.UploadFromStream(memoryStream);
}
//URL in which file is uploaded
return blockBlob.Uri.ToString();
}
/// <summary>
/// Delete the records from the logs table
/// </summary>
/// <typeparam name="T">Table type for which is retrive record</typeparam>
/// <param name="environment">Environment (UAT,PROD). Code will retrive connection string depend on environment </param>
/// <param name="startTick">starting/ from tick</param>
/// <param name="endTick">ending/ to Tick</param>
/// <param name="list">Log data retrived from table storage</param>
/// <returns>returns success or failure</returns>
private bool DeleteBulkDataFromAzureTable<T>(string environment, long startTick, long endTick, IList<T> list)
where T : TableEntity, new()
{
//Initializing storage Account with the proper storage connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[environment].ToString());
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
//Referencing table
CloudTable table = tableClient.GetTableReference(typeof(T).Name);
//Checks if table exists
if (table.Exists())
{
//Loop through each item and delete from the log table
foreach (var item in list)
{
// Create the Delete TableOperation.
if (item != null)
{
TableOperation deleteOperation = TableOperation.Delete(item);
// Execute the operation.
table.Execute(deleteOperation);
}
}
return true;
}
return false;
}