Application Insights Live Metrics with ASP.Net MVC and Web Api

两盒软妹~` 提交于 2019-12-11 07:39:43

问题


we want to monitor one of our application with Azure Application Insights. It is an ASP.Net 4 MVC application with Web Api 2. I've created a new Application Insights resource in Azure and I've installed the following NuGet packages into the project:

<package id="Microsoft.ApplicationInsights" version="2.3.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.7" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.3.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.3.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.TraceListener" version="2.2.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.Web" version="2.3.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.3.0" targetFramework="net462" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.3.0" targetFramework="net462" />

The ApplicationInsights.config looks like the following:

    <?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<!-- This key is for Application Insights resource 'PlanningTool.Web' in resource group 'Default-ApplicationInsights-CentralUS' -->
<InstrumentationKey>Our key</InstrumentationKey>
<TelemetryModules>
    <Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector" />
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.AI.PerfCounterCollector">
    <!--
    Use the following syntax here to collect additional performance counters:

    <Counters>
        <Add PerformanceCounter="\Process(??APP_WIN32_PROC??)\Handle Count" ReportAs="Process handle count" />
        ...
    </Counters>

    PerformanceCounter must be either \CategoryName(InstanceName)\CounterName or \CategoryName\CounterName

    NOTE: performance counters configuration will be lost upon NuGet upgrade.

    The following placeholders are supported as InstanceName:
        ??APP_WIN32_PROC?? - instance name of the application process  for Win32 counters.
        ??APP_W3SVC_PROC?? - instance name of the application IIS worker process for IIS/ASP.NET counters.
        ??APP_CLR_PROC?? - instance name of the application CLR process for .NET counters.
    -->
    </Add>
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector" />
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.UnhandledExceptionTelemetryModule, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.UnobservedExceptionTelemetryModule, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web">
    <Handlers>
        <!-- 
        Add entries here to filter out additional handlers: 

        NOTE: handler configuration will be lost upon NuGet upgrade.
        -->
        <Add>System.Web.Handlers.TransferRequestHandler</Add>
        <Add>Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler</Add>
        <Add>System.Web.StaticFileHandler</Add>
        <Add>System.Web.Handlers.AssemblyResourceLoader</Add>
        <Add>System.Web.Optimization.BundleHandler</Add>
        <Add>System.Web.Script.Services.ScriptHandlerFactory</Add>
        <Add>System.Web.Handlers.TraceHandler</Add>
        <Add>System.Web.Services.Discovery.DiscoveryRequestHandler</Add>
        <Add>System.Web.HttpDebugHandler</Add>
    </Handlers>
    </Add>
    <Add Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web" />
</TelemetryModules>
<TelemetryProcessors>
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector" />
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector"/>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
    <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
    </Add>
</TelemetryProcessors>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel" />
<!-- 
    Learn more about Application Insights configuration with ApplicationInsights.config here: 
    http://go.microsoft.com/fwlink/?LinkID=513840

    Note: If not present, please add <InstrumentationKey>Your Key</InstrumentationKey> to the top of this file.
-->
<TelemetryInitializers>
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.DomainNameRoleInstanceTelemetryInitializer, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer" />
    <Add Type="Microsoft.ApplicationInsights.Web.WebTestTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.SyntheticUserAgentTelemetryInitializer, Microsoft.AI.Web">
    <Filters>search|spider|crawl|Bot|Monitor|BrowserMob|BingPreview|PagePeeker|WebThumb|URL2PNG|ZooShot|GomezA|Google SketchUp|Read Later|KTXN|KHTE|Keynote|Pingdom|AlwaysOn|zao|borg|oegp|silk|Xenu|zeal|NING|htdig|lycos|slurp|teoma|voila|yahoo|Sogou|CiBra|Nutch|Java|JNLP|Daumoa|Genieo|ichiro|larbin|pompos|Scrapy|snappy|speedy|vortex|favicon|indexer|Riddler|scooter|scraper|scrubby|WhatWeb|WinHTTP|voyager|archiver|Icarus6j|mogimogi|Netvibes|altavista|charlotte|findlinks|Retreiver|TLSProber|WordPress|wsr-agent|http client|Python-urllib|AppEngine-Google|semanticdiscovery|facebookexternalhit|web/snippet|Google-HTTP-Java-Client</Filters>
    </Add>
    <Add Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.AuthenticatedUserIdTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.AccountIdTelemetryInitializer, Microsoft.AI.Web" />
    <Add Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" />
</TelemetryInitializers>
</ApplicationInsights>

But Application Insights does for exmample not show the live metrics or the server requests.

I've tried the access to App Insights, via a test program, which is woring, all data (Live metrics, requests, etc.) were shown correctly: using Microsoft.ApplicationInsights; using System;

namespace AppInsightsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Application Insights Test");
            var e = new Exception("This is a App Insights test");
            var ai = new TelemetryClient();
            ai.TrackException(e);
            Console.WriteLine("Finished...");
            Console.ReadKey();
        }
    }
}

How do I have to configure App Insights in an MVC/Web Api project so that it works correctly.

Thanks in advance.

Update with screenshots form Visual Studio (2017) and Azure Portal: VS:

Azure Portal (in german):


回答1:


After ignoring this task for a long time I've finally found the answer. The service is hosted behind a company proxy and to enable it send the data to azure you've to add the proxy to your web.config:

<configuration>
     <system.net enabled="true" useDefaultCredentials="true">
         <proxy proxyaddress="http://url.to.your.proxy" bypasslocal="true" />
     </system.net>
</configuration>

You have to adopt the settings dependent of your proxy configuration.




回答2:


I add Application Insights to my existing MVC project with the following steps, I can get live metrics stream and server requests metrics, please refer to it.

  • Right-click web app project in Solution Explorer, and choose Add > Application Insights Telemetry... , and then register app with Application Insights and configure Application Insights. (Which will help us install Application Insights related packages and configure projects.)

  • Open web pages to generate some telemetry

  • On Azure portal, I can see the number of online servers and Live metrics stream.

  • Add chart to show server requests

Besides, you could also refer to this article that shows how to configure your ASP.NET web app to send telemetry to the Azure Application Insights service.




回答3:


Your Azure application insights resource is not linked to your web application.

In Azure portal find your Application Insights resource and navigate to Configure->Properties.

There you will find "INSTRUMENTATION KEY".

Copy that key to your applicationInsights.config section:

<InstrumentationKey>Our key</InstrumentationKey>


来源:https://stackoverflow.com/questions/44701228/application-insights-live-metrics-with-asp-net-mvc-and-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!