我們在使Windows Azure Diagnostics可靠登錄時遇到問題。這看起來很難,我們不明白爲什麼。爲什麼Windows Azure診斷程序不能可靠地登錄?
這裏是我們的代碼,有時有效,有時不能:
public class WorkerRole : RoleEntryPoint
{
public override void Run()
{
Trace.WriteLine("Run() beginning.", LogLevel.Information.ToString());
try
{
var logic = new WorkerAgent();
logic.Go(false);
}
catch (Exception err)
{
Trace.WriteLine(err.ToString(), LogLevel.Critical.ToString());
Run();
}
}
public override bool OnStart()
{
// Initialize our Cloud Storage Configuration.
AzureStorageObject.Initialize(AzureConfigurationLocation.AzureProjectConfiguration);
// Initialize Azure Diagnostics
try
{
//get the storage account using the default Diag connection string
var cs = CloudStorageAccount.FromConfigurationSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
//get the diag manager
var dm = cs.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId,
RoleEnvironment.CurrentRoleInstance.Role.Name,
RoleEnvironment.CurrentRoleInstance.Id);
//get the current configuration but if that failed, get the values from config file
var dc = dm.GetCurrentConfiguration() ?? DiagnosticMonitor.GetDefaultInitialConfiguration();
//Windows Azure Logs
dc.Logs.BufferQuotaInMB = 25;
dc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
dc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//Windows Event Logs
dc.WindowsEventLog.BufferQuotaInMB = 25;
dc.WindowsEventLog.DataSources.Add("System!*");
dc.WindowsEventLog.DataSources.Add("Application!*");
dc.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
////Performance Counters
//dc.PerformanceCounters.BufferQuotaInMB = 25;
//var perfConfig = new PerformanceCounterConfiguration
// {
// CounterSpecifier = @"\Processor(_Total)\% Processor Time",
// SampleRate = TimeSpan.FromSeconds(60)
// };
//dc.PerformanceCounters.DataSources.Add(perfConfig);
//dc.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//Failed Request Logs
dc.Directories.BufferQuotaInMB = 25;
dc.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
////Infrastructure Logs
//dc.DiagnosticInfrastructureLogs.BufferQuotaInMB = 25;
//dc.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
//dc.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//Crash Dumps
CrashDumps.EnableCollection(true);
//overall quota; must be larger than the sum of all items
dc.OverallQuotaInMB = 5000;
//save the configuration
dm.SetCurrentConfiguration(dc);
}
catch (Exception ex)
{
Trace.Write(ex.Message, LogLevel.Critical.ToString());
}
// give logging time to register itself and load up.
Thread.Sleep(10000);
Trace.WriteLine("Completed diagnostics initialization.", LogLevel.Information.ToString());
return base.OnStart();
}
}
請注意,我們AzureStorageObject.Initialize
方法取代了標準CloudStorageAccount.SetConfigurationSettingPublisher
方法。
使用此代碼絕對沒有更改代碼或配置的變化,我們可以在模擬器中一遍又一遍又一遍運行或以上,並一遍又一遍地部署它同樣不可靠的結果到Azure。請注意,什麼是應該發生的是1)設置WAD 2)睡眠10秒給它的時間來完成(我是真的抓住了救命稻草,當我加入這個)3)日誌WAD初始化完成4)我們日誌Run()
被稱爲然後我們去做所有的工作(WorkerAgent
有我們的while(true)
循環)。有時候這是事情發生的。有時,我們沒有得到3)中記錄的信息,但我們確實在4)中得到了它。有時我們不會在3或4)。同樣,代碼或配置中沒有任何更改,所有這些都指向Azure存儲(而非模擬器存儲)。
爲什麼沒有這種可靠記錄每次我們打電話Trace.Write
時間?
是需要時間的診斷東西避開復制你的日誌存儲。你的角色持續超過15分鐘嗎? –
是的。看看配置 - 我有它設置每1分鐘複製它們。未來的項目已成功記錄,這是我啓動實例時隨機未記錄的這些初始項目。正如「Thread.Sleep(10000)」所證明的那樣,我不確定當它開始成功登錄時,決定性因素是什麼。有時它是即時的,並且記錄第一條消息而沒有睡眠,並且有時甚至在睡眠時它不會記錄「一段時間」的任何消息(我不知道「一段時間」是什麼,或者爲什麼「需要」一段時間「)。 – Jaxidian
不幸的是,我不知道同樣的事情,所以我投了你的問題。我最終寫了我自己的表格存儲記錄器...有時它由於存儲http打嗝失敗。 – CedricB