简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM