ApplicationInsights dotnet核心自定义初始化程序

时间:2017-04-05 13:05:34

标签: asp.net-core .net-core azure-application-insights

我无法弄清楚如何为aspnetcore项目配置AI。我做了以下事情:

services.AddSingleton<ITelemetryInitializer, AppInsightsInitializer>();
services.AddApplicationInsightsTelemetry(Configuration);

我需要登录用户和servicename所以我有这个初始化程序:

public class AppInsightsInitializer : ITelemetryInitializer
{
    private IHttpContextAccessor _httpContextAccessor;
    public AppInsightsInitializer(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException("httpContextAccessor");
    }
    public void Initialize(ITelemetry telemetry)
    {
        var httpContext = _httpContextAccessor.HttpContext;
        telemetry.Context.Properties["appname"] = "MyCoolService";

        if (httpContext != null && httpContext.User.Identity.IsAuthenticated == true && httpContext.User.Identity.Name != null)
        {
            telemetry.Context.User.AuthenticatedUserId = httpContext.User.Identity.Name;
        }
    }

}

我没有applicationinights.config文件(我知道不需要它们)

问题:我每个日志有4个条目(相同的ID)。数据是正确的。我在日志中也遇到了以下错误:

AI: Error collecting 9 of the configured performance counters. Please check the configuration.  Counter \Process(??APP_WIN32_PROC??)\% Processor Time: Failed to perform the first read for performance counter. Please make sure it exists. 
Category: Process, counter: % Processor Time, instance Counter \Memory\Available Bytes: Failed to register performance counter. 
Category: Memory, counter: Available Bytes, instance: . 
Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec: Failed to perform the first read for performance counter. Please make sure it exists. 
Category: ASP.NET Applications, counter: Requests/Sec, instance MyCoolv3.Api.exe Counter \.NET CLR Exceptions(??APP_CLR_PROC??)\# of Exceps Thrown / sec: Failed to perform the first read for performance counter. Please make sure it exists. 
Category: .NET CLR Exceptions, counter: # of Exceps Thrown / sec, instance Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time: Failed to perform the first read for performance counter. Please make sure it exists. 
Category: ASP.NET Applications, counter: Request Execution Time, instance MyCoolv3.Api.exe Counter \Process(??APP_WIN32_PROC??)\Private Bytes: Failed to perform the first read for performance counter. Please make sure it exists. Category: Process, counter: Private Bytes, instance Counter \Process(??APP_WIN32_PROC??)\IO Data Bytes/sec: Failed to perform the first read for performance counter. Please make sure it exists. 
Category: Process, counter: IO Data Bytes/sec, instance Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue: Failed to perform the first read for performance counter. Please make sure it exists. Category: ASP.NET Applications, counter: Requests In Application Queue, instance MyCoolv3.Api.exe 
Counter \Processor(_Total)\% Processor Time: Failed to register performance counter. Category: Processor, counter: % Processor Time, instance: _Total.

我正在使用:

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.0-beta1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />

1 个答案:

答案 0 :(得分:1)

services.AddApplicationInsightsTelemetry(Configuration);
不再需要

(假设您甚至应该在该行上获得弃用警告?)

如果你在VS2017中创建了一个asp.net核心新项目,那么AI已经存在于包引用中(虽然是2.0版本,而不是那个2.1 beta版本),并且所有的连接都已经在{{1在其他一些文件中。

如果您要移植现有的,而不是上面的program.cs行,而不是

AddApplicationInsights...

在你的应用的.UseApplicationInsights() 启动中。有关详细信息,请参阅2.1 beta release notes on github

中的相关信息

我们还在努力更新VS2017中的“配置应用程序洞察”工具,以便在将来的更新中正确“迁移”这样的应用程序。

我不确定为什么你会得到任何事件的多个实例,除非你明确记录它们,或者你可能有多次启动调用(也不应该影响任何事情)。你在哪里看到多个实例?在VS的appinsights工具?在门户网站?