gpt4 book ai didi

azure - 为什么 Windows Azure 诊断不能可靠地记录日志?

转载 作者:行者123 更新时间:2023-12-04 21:23:38 25 4
gpt4 key购买 nike

我们在使 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) sleep 10 秒,让它有时间完成(添加此内容时我真的捕获了救命稻草) 3) 记录 WAD 初始化已完成 4) 我们记录 Run() 被调用,然后我们开始执行所有工作(WorkerAgent 中有我们的 while(true) 循环)。有时会发生这种情况。有时,我们在 3) 中没有收到记录的消息,但在 4) 中却收到了。有时我们在 3 或 4) 中无法得到它。同样,代码或配置没有任何变化,所有这些都指向 Azure 存储(不是模拟器存储)。

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

最佳答案

这个问题

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

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

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

关于azure - 为什么 Windows Azure 诊断不能可靠地记录日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7522762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com