2017-04-05 52 views
2

我無法弄清楚如何爲aspnetcore項目配置AI。我已經做了以下內容:ApplicationInsights dotnet核心自定義初始化程序

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

當我需要的用戶的loggedIn和服務名,所以我有了這個初始化:

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)。數據是正確的。我也有在日誌下面errror:

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
services.AddApplicationInsightsTelemetry(Configuration); 

不需要了(只是假設,你應該甚至可以在該行獲得廢棄警告?)

如果你在VS2017中創建了一個asp.net核心新項目,AI已經在包引用中(儘管2.0版本,而不是2.1 beta版本),並且所有的連接都已經在program.cs和其他一些文件。

如果要移植現有一個,然後代替上述AddApplicationInsights...線,而是你必須

.UseApplicationInsights() 

program.cs啓動您的應用程序,而不是。更多詳細信息,請參閱2.1 beta release notes on github

我們也在更新VS2017中的「配置應用程序洞察」工具,以便在未來的更新中正確「遷移」這樣的應用程序。

我不確定爲什麼你會得到任何事件的多個實例,除非你明確地記錄它們,或者如果你可能有多個調用啓動(這也應該不會影響任何東西)。你在哪裏看到多個實例?在VS的appinsights工具?在門戶網站?

+0

相關問題:如果我想以編程方式在Program.cs中的WebBuilder上使用UseApplicationInsights()時添加遙測密鑰,但在啓動配置(這是我從keyvault加載遙測密鑰的位置)之前執行的。我想過在WebBuilder上調用UseConfiguration(xx)並從xx中檢索配置,然後將其用作UseApplicationInsights的參數......想法? – MarkD