将Application Insights集成到Service Fabric中,可以通过以下步骤完成:
<Parameters>
元素中添加以下配置:<Parameter Name="ApplicationInsightsKey" DefaultValue="[Your_Application_Insights_Instrumentation_Key]" />
将[Your_Application_Insights_Instrumentation_Key]
替换为在第一步中创建的Application Insights资源的仪表板中的仪表板密钥。
<DefaultServices>
元素中添加以下代码:<Service Name="TelemetryService">
<StatelessService ServiceTypeName="TelemetryServiceType" InstanceCount="[Number_of_Instances]">
<SingletonPartition />
<Extensions>
<Extension Name="TelemetryServiceExtension" Type="TelemetryService.TelemetryServiceExtension" />
</Extensions>
</StatelessService>
</Service>
将[Number_of_Instances]
替换为适配器实例的数量。
using Microsoft.ApplicationInsights.ServiceFabric;
using Microsoft.ServiceFabric.Services.Runtime;
namespace TelemetryService
{
internal static class Program
{
private static void Main()
{
TelemetryConfiguration.Active.InstrumentationKey = ServiceInitialization.GetInstrumentationKey();
ServiceRuntime.RegisterServiceAsync("TelemetryServiceType", context => new TelemetryService(context)).GetAwaiter().GetResult();
ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(TelemetryService).Name);
Thread.Sleep(Timeout.Infinite);
}
}
}
public static class ServiceInitialization
{
public static string GetInstrumentationKey()
{
var activationContext = FabricRuntime.GetActivationContext();
var configPackageName = "Config";
var configPackage = activationContext.GetConfigurationPackageObject(configPackageName);
var telemetryConfig = configPackage.Settings.Sections["TelemetryConfig"];
return telemetryConfig.Parameters["InstrumentationKey"].Value;
}
}
完成上述步骤后,Application Insights将与Service Fabric应用程序集成。您可以在Application Insights仪表板中查看应用程序的性能和日志数据,以及进行故障排除和监控。
腾讯云相关产品推荐:腾讯云监控服务(https://cloud.tencent.com/product/monitoring)