Search code examples
c#azureasp.net-web-apiazure-application-insightstelemetry

How to log ASP.NET Web API request body into a Application Insights failure?


I'm logging my unhandled exceptions in an ExceptionLogger using the TelemetryClient to Application Insights on Azure.

public class GlobalExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        if (context != null && context.Exception != null)
        {
              //For simplification a new TelemetryClient instance
              //This is not recommended!
              new TelemetryClient().TrackException(context.Exception);
        }
        base.Log(context);
    }
}

Is there a way to log the Web API Request body so I can view it on the Application Insights dashboard on the Azure Portal?


Solution

  • You can create ExceptionTelemetry instance and add custom properties. Then call general Track method.

    var telemetry = new ExceptionTelemetry(context.Exception);
    telemetry.Properties.Add("name", "value");
    new TelemetryClient().Track(telemetry);