Last active
December 31, 2015 02:58
-
-
Save RhysC/7924004 to your computer and use it in GitHub Desktop.
Gets all the blobs in wad-log4net from the last 24 hours (by last mod date). Also can be found here for easy linq pad use : http://share.linqpad.net/hltnqf.linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var accounts = new Dictionary<string,string>{ | |
{"accountname", "accountkey"}, | |
}; | |
var latestLogsByAppName = GetLatestLogsByAppName(accounts); | |
latestLogsByAppName//.Where (l => !l.ApplicationName.Contains("WebRole") ) | |
//.Where(l => l.LastModified < DateTime.UtcNow.AddDays(-1)) | |
.OrderBy (l => l.LastModified) | |
.Dump(); | |
// var path = logContainer.DownloadLastBlobFromContainer(b=>b.Name.Contains("TerminalGatewayReadModelPopulator")); | |
// path.Dump(); | |
} | |
public IEnumerable<BlobSummary> GetLatestLogsByAppName(Dictionary<string,string> accounts) | |
{ | |
foreach(var account in accounts) | |
{ | |
var cloudBlobClient = CreateCloudBlobClient(account.Key, account.Value); | |
var logContainer = cloudBlobClient.GetContainerReference("wad-log4net"); | |
var latestlogsForThisContainer = logContainer.GetBlobsFromContainer() | |
.OrderByDescending (x=> x.LastModified) | |
.GroupBy (x => x.ApplicationName) | |
.SelectMany (x => x.Take(1)); | |
foreach (var log in latestlogsForThisContainer) | |
{ | |
yield return log; | |
} | |
} | |
} | |
public CloudBlobClient CreateCloudBlobClient(string accountName, string accountKey) | |
{ | |
var storageConStr = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, accountKey); | |
var account = CloudStorageAccount.Parse(storageConStr); | |
var cloudBlobClient = account.CreateCloudBlobClient(); | |
return cloudBlobClient; | |
} | |
public static class Ex | |
{ | |
public static IEnumerable<BlobSummary> GetBlobsFromContainer(this CloudBlobContainer container) | |
{ | |
var blobs = container.ListBlobsSegmented(string.Empty, true, BlobListingDetails.Snapshots, null, null, null, null) | |
.Results | |
.Cast<CloudBlockBlob>(); | |
return blobs.Select (b => new BlobSummary { | |
ApplicationName=b.Name.Split('/').Last().Split('.').First (), | |
LastModified = b.Properties.LastModified.GetValueOrDefault().ToLocalTime(), | |
FileLength = b.Properties.Length, | |
Uri = b.Uri | |
}); | |
} | |
private static string GetFileSize(long filelength) | |
{ | |
string[] sizes = { "B", "KB", "MB", "GB" }; | |
var len = filelength; | |
int order = 0; | |
while (len >= 1024 && order + 1 < sizes.Length) { | |
order++; | |
len = len/1024; | |
} | |
return string.Format("{0:0.##} {1}", len, sizes[order]); | |
} | |
public static string DownloadLastBlobFromContainer(this CloudBlobContainer container, Func<CloudBlockBlob,bool> whereClause = null ) | |
{ | |
var blobQuery = container.ListBlobsSegmented(string.Empty, true, BlobListingDetails.Snapshots, null, null, null, null) | |
.Results | |
.Cast<CloudBlockBlob>(); | |
if(whereClause != null) | |
blobQuery = blobQuery.Where(whereClause); | |
var blob = blobQuery.OrderByDescending (x=> x.Properties.LastModified) | |
.First(); | |
blob.Name.Dump("Downloading:"); | |
var blobcontents = blob.DownloadText(); | |
var tempPath = Path.GetTempFileName(); | |
File.AppendAllText(tempPath, blobcontents); | |
return tempPath; | |
} | |
public static void DeleteBlobsFromContainer(this CloudBlobContainer container, Func<CloudBlockBlob,bool> whereClause = null ) | |
{ | |
var blobQuery = container.ListBlobsSegmented(string.Empty, true, BlobListingDetails.Snapshots, null, null, null, null) | |
.Results | |
.Cast<CloudBlockBlob>(); | |
if(whereClause != null) | |
blobQuery = blobQuery.Where(whereClause); | |
var blobs = blobQuery.OrderByDescending (x=> x.Properties.LastModified); | |
blobs.Select (b => new{b.Name, b.Properties.LastModified}).Dump("To delete :"); | |
foreach (var blob in blobs) | |
{ | |
blob.Delete(); | |
} | |
} | |
} | |
public class BlobSummary | |
{ | |
public string Service { get{ return Uri.Host.Split('.').First();}} | |
public string ApplicationName { get; set; } | |
public string FileSize { get{ return GetFileSize(FileLength); } } | |
public string Date { get{ return LastModified.Value.ToString("dd MMM");}} | |
public TimeSpan TimeOfday { get{ return LastModified.Value.TimeOfDay;}} | |
public DateTimeOffset? LastModified { get; set; } | |
public long FileLength { get; set; } | |
public Uri Uri { get; set; } | |
private static string GetFileSize(long filelength) | |
{ | |
string[] sizes = { "B", "KB", "MB", "GB" }; | |
var len = filelength; | |
int order = 0; | |
while (len >= 1024 && order + 1 < sizes.Length) { | |
order++; | |
len = len/1024; | |
} | |
return string.Format("{0:0.##} {1}", len, sizes[order]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment