我们在让 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
?