我正在尝试运行一个 azure 角色 - 它在模拟器上运行,但不在 Azure 上。所以我尝试将 Tracing 集成到我的项目中。我首先用一个简单的测试项目(效果很好)进行了尝试,但是在我的“真实”项目中我没有得到结果。
这是我的文件:
应用程序配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>
</configuration>
服务配置.Cloud.cscfg
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="MyServiceName" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="3" osVersion="*" schemaVersion="2013-03.2.0">
<Role name="MyRoleName">
<Instances count="2" />
<ConfigurationSettings>
<Setting name="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<myAccount>;AccountKey=<myKEy==>" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<myAccount>;AccountKey=<myKEy==>" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
服务定义.csdef
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyServiceName" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2013-03.2.0">
<WorkerRole name="MyRoleName" vmsize="Small">
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
<Endpoints>
<InternalEndpoint name="InternalEndpoint1" protocol="tcp" />
</Endpoints>
<ConfigurationSettings>
<Setting name="StorageConnectionString" />
</ConfigurationSettings>
</WorkerRole>
</ServiceDefinition>
我的源代码(片段):
public override bool OnStart()
{
ConfigDiagnostics();
Trace.TraceInformation("Initializing storage account");
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
return base.OnStart();
}
private void ConfigDiagnostics()
{
DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();
config.ConfigurationChangePollInterval = TimeSpan.FromMinutes(1d);
config.Logs.BufferQuotaInMB = 500;
config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d);
//DiagnosticMonitor.Start(@"StorageConnectionString", config);
DiagnosticMonitor.Start(@"Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);
}
相同的代码在一个普通的新 Azure 项目中有效——但在这个项目中无效。我现在能发现的唯一区别是:我的“真实”项目在我的 run() 方法中引发了异常。可能是我的跟踪结果(来自 Trace.WriteLine 或 Trace.TraceError 或 Trace.TraceInformation)没有上传,因为 azure 实例的异常和回收发生得太快/太早,无法将我的跟踪结果上传到我的存储帐户?还是我错过了另一点?!
此致 :)