简体   繁体   中英

Application Insights not tracking exceptions when used in ServiceStack application

I currently have a problem where App Insights isn't showing the exceptions in .Net Core. I am also using ServiceStackCore to build my API.

This is what it currently looks like in the Azure portal under Application Insights: Screenshot of Azure portal

As you can see, the response codes all show 400, 403, 500. But there are no exceptions:

I have found a round-about route to get the Exceptions:

var telemetry = new TelemetryClient();
...
try
{ ...
}
catch (Exception ex)
{
   telemetry.TrackException(ex);
}
  • I am wanting to know if ServiceStack has any inbuilt exception handling that might be muting exceptions that are supposed to be caught by app insights?

  • And if there is any out of the box configuration that can be done in App Insights to show these exceptions without having to add the try-catch block?

ServiceStack lets you register handlers to handle Exceptions :

public override void Configure(Container container)
{
    //Handle Exceptions occurring in Services:

    this.ServiceExceptionHandlers.Add((httpReq, request, exception) => {
        //log your exceptions here
        ...
        return null; //continue with default Error Handling

        //or return your own custom response
        //return DtoUtils.CreateErrorResponse(request, exception);
    });

    //Handle Unhandled Exceptions occurring outside of Services
    //E.g. Exceptions during Request binding or in filters:
    this.UncaughtExceptionHandlers.Add((req, res, operationName, ex) => {
         res.Write($"Error: {ex.GetType().Name}: {ex.Message}");
         res.EndRequest(skipHeaders: true);
    });
}

ServiceExceptionHandlers is fired for Service Exceptions whilst UncaughtExceptionHandlers are fired for non or unknown Service Requests.

Thank you for your answer and to add more claririty:

I have been overriding the HandleUncaughtException method as well as the ResolveResponseException method in AppHost in order to

  • sanitize serialization errors as bad requests
  • sanitize exceptions that I do not want to expose to clients (as internal server error)

     private TelemetryClient telemetry = new TelemetryClient(); public override void HandleUncaughtException(IRequest httpReq, IResponse httpRes, string operationName, Exception ex) { telemetry.TrackException(ex); if (!(ex is SerializationException)) { // don't handle like BadRequest if not serialization exception base.HandleUncaughtException(httpReq, httpRes, operationName, ex); return; } httpRes.WriteError(new HttpError(HttpStatusCode.BadRequest, ex.InnerException?.Message), (int)HttpStatusCode.BadRequest); httpRes.EndRequest(skipHeaders: true); } public override Exception ResolveResponseException(Exception ex) { telemetry.TrackException(ex); if (ex.GetType() == typeof(HttpError)) { // Exception thrown using HttpError, do not sanitize return ex; } return new HttpError(HttpStatusCode.InternalServerError, "The service has encountered an error, please contact your account manager if this persists."); } 

And also I have added TrackException() telemetry to both methods which will cause the exceptions to get tracked to App Insights.

I think ServiceStack is somehow suppressing the exceptions from showing up in App Insights, and this implementation fixes that. The reason why I think this might be happening is because I also have another API that is run without ServiceStack (IdentityServer) which correctly tracks the exceptions.

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