为什么Windows Azure Diagnostics无法可靠地记录日志? [英] Why doesn't Windows Azure Diagnostics reliably log?

查看:54
本文介绍了为什么Windows Azure Diagnostics无法可靠地记录日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在使Windows Azure诊断程序可靠记录方面遇到问题.看来是命中注定,我们也不知道为什么.

We are having problems getting Windows Azure Diagnostics to reliably log. It seems hit-or-miss and we don't understand why.

这是我们的代码有时有效,有时无效:

Here's our code that sometimes works, sometimes doesn't:

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 方法.

Note that our AzureStorageObject.Initialize method replaces the standard CloudStorageAccount.SetConfigurationSettingPublisher method.

使用此代码时,无需进行任何代码更改或配置更改,我们可以在模拟器中一遍又一遍地运行它,或一遍又一遍地将其部署到Azure上,结果同样不可靠.请注意,应该发生的是1)设置WAD 2)睡眠10秒钟,以使其有时间完成(添加时我真的抓着稻草)3)记录WAD初始化已完成4)我们记录该Run()被调用,然后我们去做所有工作( WorkerAgent 中有我们的 while(true)循环).有时这就是发生的情况.有时,我们在3)中没有得到记录的消息,但在4)中却得到了.有时我们在3或4中没有得到它.再次说明,代码或配置方面没有什么变化,所有这些都指向Azure存储(不是模拟器存储).

Using this code with absolutely no code changes or configuration changes, we can run it over and over and over in the emulator or deploy it over and over and over to Azure with equally unreliable results. Notice that what's SUPPOSED to happen is 1) setup WAD 2) sleep 10 seconds to give it time to finish (I was really grasping for straws when I added this) 3) log that WAD init is done 4) we log that Run() is called and then we go do all of our work (WorkerAgent has our while(true) loop in it). Sometimes this is what happenes. Sometimes, we don't get the logged message in 3) but we do get it in 4). Sometimes we don't get it in 3 or 4). Again, NOTHING changes in code or configuration and all of this points to Azure storage (not emulator storage).

为什么每次我们调用 Trace.Write 时,日志记录都不可靠?

Why isn't this reliably logging every time we call Trace.Write?

推荐答案

这个问题

TraceSource.TraceEvent()在出现异常时无法记录邮件中包含不可打印的字符

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

reports an issue when logging silently fails as a consequence of an exception being thrown while logging. Specifically in this case the log message can't be serialized.

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

The fix for this situation is to use HttpUtility.HtmlEncode to encode the exception text before it's logged in Azure.

这篇关于为什么Windows Azure Diagnostics无法可靠地记录日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆