将应用程序洞察力添加到无状态服务结构应用程序

Add application insights to stateless service fabric app

我正在尝试按照 this documentation 开始在我部署到 Service Fabric 的 .net 核心应用程序中使用应用程序洞察力。

我的代码真的很简单

public FailedAuthorise(StatelessServiceContext context, IConfigManager config)
        : base(context)
{
    _worker = new Worker<PaymentFailedAuthorise>(config, FailedAuthoriseHandlerFactory.Create, "FailedAuthorise", "FailedAuthoriseError");
}

    protected override async Task RunAsync(CancellationToken cancellationToken)
{
    await _worker.RunAsync(cancellationToken);
}

作为工作人员只是一个通用的 class 从一些队列中读取并处理消息

但是如果我要按照该文档进行操作,我将需要安装一些 nuget 包(这实际上给我带来了问题,无法找到 and/or 安装,例如无法访问 使用 Microsoft.ApplicationInsights.ServiceFabric; 或将需要在管道上的配置文件修改中更改检测密钥)并开始使用我并不真正需要的 "hosting" classes解决方案。

这不是一种简单的方法,只需将应用程序洞察力添加到云服务中曾经是工作者角色而无需托管位吗?

谢谢,

您只需添加 this nuget package 并像这样创建您自己的自定义遥测数据:

public class MyService
{
    private readonly TelemetryClient telemetryClient;

    public MyService()
    {
        telemetryClient = new TelemetryClient(configuration);
        telemetryClient.Context.InstrumentationKey = "[Your Key Here]";
    }

    public FailedAuthorise(StatelessServiceContext context, IConfigManager config)
            : base(context)
    {
        _worker = new Worker<PaymentFailedAuthorise>(config, FailedAuthoriseHandlerFactory.Create, "FailedAuthorise", "FailedAuthoriseError");
    }

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        telemetryClient.TrackEvent("Worker started");
        await _worker.RunAsync(cancellationToken);
    }
}

您可以跟踪几件事,例如 exceptions, traces, events, metrics and requests,但如果您不使用 Asp.Net Core,则必须手动发送这些事件,而不是让一些中间件将遥测数据发送到 App洞察力。

如果您的服务调用其他服务,您可以添加 this package 以自动跟踪与其他服务的通信。