为什么Windows Azure Diagnostics无法可靠地记录?

时间:2011-09-22 23:40:57

标签: azure 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)我们记录{{1调用然后我们继续完成所有工作(Run()包含我们的WorkerAgent循环)。有时这就是发生的事情。有时,我们没有在3)中获取记录的消息,但我们确实在4)中得到了它。有时我们不会在3或4中得到它。同样,代码或配置中没有任何变化,所有这些都指向Azure存储(而不是模拟器存储)。

为什么每次拨打while(true)时都没有可靠的记录?

1 个答案:

答案 0 :(得分:2)

这个问题

TraceSource.TraceEvent() fails logging when Exception message contains non-printable characters

报告由于在记录时抛出异常导致静默登录失败的问题。特别是在这种情况下,日志消息无法序列化。

此情况的解决方法是使用HttpUtility.HtmlEncode在异常文本登录Azure之前对其进行编码。