加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:48:57 所属栏目:Windows 来源:网络整理
导读:我们在使 Windows Azure诊断程序可靠地记录时遇到问题.它似乎有点不可思议,我们不明白为什么. 这是我们的代码,有时可以工作,有时不会: public class WorkerRole : RoleEntryPoint{ public override void Run() { Trace.WriteLine("Run() beginning.",LogLev
我们在使 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中得到它.同样,代码或配置中的NOTHING更改,所有这些都指向Azure存储(而不是模拟器存储).

为什么每次调用Trace.Write时都不能可靠地记录?

解决方法

这个问题

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读