Insights Does Not Track Exceptions When Used in ServiceStack

I currently have a problem where App Insights does not show exceptions in .Net Core. I also use ServiceStackCore to create my API.

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

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

I found a round route to get Exceptions:

var telemetry = new TelemetryClient();
...
try
{ ...
}
catch (Exception ex)
{
   telemetry.TrackException(ex);
}
  • I want to know if ServiceStack has any kind of built-in exception handling that might be throwing an exception that should be detected as a result of an application search?

  • And if in App Insights there is any configuration from the window to show these exceptions without adding a try-catch block?

+4
2

ServiceStack :

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 , UncaughtExceptionHandlers - .

+2

:

HandleUncaughtException, ResolveResponseException AppHost,

  • , ( )

    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.");
    }
    

TrackException() , - App Insights.

, ServiceStack - App Insights, . , , , , API, ServiceStack (IdentityServer), .

0

Source: https://habr.com/ru/post/1693305/


All Articles