Application Insights - micklpl/avanade-azure-workshop GitHub Wiki

Available tags

ai-request-timeline ai-telemetry-client

Configuration

git checkout ai-request-timeline

Open Visual Studio and install Microsoft.ApplicationInsights.Web Nuget package using Package Manager Console typing:

Install-Package Microsoft.ApplicationInsights.Web -Version 2.4.1

Deploy your Application to Azure and make sure that your Web App Application Settings has APPINSIGHTS_INSTRUMENTATIONKEY filled

From Web App menu select Application Insights and restart your application

You can add

 <Channel>
   <MaxTelemetryBufferCapacity>5</MaxTelemetryBufferCapacity>
 </Channel>

in ApplicationInsights.config file to force AI to flush buffer more frequently.

Play around with your application. Open different team and player profiles. After few minutes you should be able to see some metrics on Application Insights main page:

End-to-end transaction details

Click Search on top bar

Type in PlayerDetails and uncheck Dependencies to get only request metrics.

Open Single entry

Analyze all dependency calls. Try to open that page again (having images cached in blob storage) and compare timelines.

Exceptions

Open FilterConfig.cs and replace default Exception handling with following implementation:

   public class FilterConfig
   {
       public static void RegisterGlobalFilters(GlobalFilterCollection filters)
       {
           filters.Add(new AiHandleErrorAttribute());
       }
   }

Providing following implementation:

   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
   public class AiHandleErrorAttribute : HandleErrorAttribute
   {
       public override void OnException(ExceptionContext filterContext)
       {
           if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
           {
                var ai = new TelemetryClient();
                ai.TrackException(filterContext.Exception);
           }
           base.OnException(filterContext);
        }
   }

Try to draw few games for Group 3. Once you get an exception browse to Azure Portal and open Application Insights.

Try to find out what happened and which method in application's logic fails.

Telemetry Client

git checkout ai-telemetry-client

Open CorrelationIdFilter class and consider it's purpose. Open TelemetryService and add following class body.

public class TelemetryService
{
    private readonly TelemetryClient _telemetryClient;

    public TelemetryService()
    {
        _telemetryClient = new TelemetryClient();
    }

    public void Log(string message, string correlationId)
    {
        _telemetryClient.TrackTrace($"{message}, {nameof(correlationId)}: {correlationId}");
    }
}

Deploy your application. Open Chrome Dev Tools and click Network tab

Play one game. Observe Network tab. Try to find PlayGame request details. Find X-Correlation-Id response header.

Paste correlationId to Application Insights.

⚠️ **GitHub.com Fallback** ⚠️