我需要使用C#代码在文本文件中为Android应用程序下载azure日志我如何以编程方式或从azure portal手动执行此操作是否有任何API可以访问Microsoft Azure应用程序日志?
请参阅附件图片以供参考,我需要在文本文件中下载这些日志。
答案 0 :(得分:1)
您必须在c#应用程序中使用 Microsoft.WindowsAzure 命名空间。
这里有一些下载IIS日志文件的代码:
string directory = "[path to save files]";
string instance = "[Azure instance name]";
string key = "[Azure key]";
var account = new CloudStorageAccount(
new StorageCredentialsAccountAndKey(
instance,
key
),
false
);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("wad-iis-logfiles");
//note that I pass the BlobRequestOptions of UseFlatBlobListing which returns all files regardless
//of nesting so that I don't have to walk the directory structure
foreach (var blob in container.ListBlobs(new BlobRequestOptions() { UseFlatBlobListing = true }))
{
CloudBlob b = blob as CloudBlob;
try
{
b.FetchAttributes();
BlobAttributes blobAttributes = b.Attributes;
TimeSpan span = DateTime.Now.Subtract(blobAttributes.Properties.LastModifiedUtc.ToLocalTime());
int compare = TimeSpan.Compare(span, TimeSpan.FromHours(1));
//we don't want to download and delete the latest log file, because it is incomplete and still being
//written to, thus this compare logic
if (compare == 1)
{
b.DownloadToFile(directory + b.Uri.PathAndQuery);
b.Delete();
}
}
catch (Exception e)
{
Console.WriteLine(instance + " download of logs failed!!!!" + e.Message);
return;
}
}
Console.WriteLine(instance + " download of logs complete at " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + "");
以下是如何从任何Azure实例下载和删除日志的示例:https://code.msdn.microsoft.com/windowsdesktop/Azure-Log-Fetcher-522ff173