API

Storing handled exceptions

To record exceptions handled by a try-catch block, use RevDeBugAPI.Snapshot.RecordException and pass the exception object as a parameter. For example:

try
    {
        var product = new Product();
        product.convertData(dataFile);
    }
    catch (Exception e){
        // The exception will be recorded by RevDeBug
        RevDeBugAPI.Snapshot.RecordException(e);
    }

The recorded snapshot of code execution that led to the exception will be stored on a Recording Server and visible on the RevDeBug DevOps Monitoring dashboard in the same manner unhandled exceptions are.

Recordings of complex objects

To record inside an object of any class use RevDeBugAPI.RecordObject(object). This function records the interior of the object at a given moment of the program operation.

var product = new Product();
product.convertData(dataFile);

// The object will be recorded by RevDeBug
RevDeBugAPI.RecordObject(product);

Using the RecordSnapshot function

RevDeBug offers the possibility to create code recordings even when there is no unexpected exception. To use the function of creating snapshots, you should use the RevDeBugAPI.Snapshot.RecordSnapshot(RecordingName) function anywhere in the program code. The place in the code where we used the snapshot function will be the end of the recording.

var product = new Product();
product.convertData(dataFile);

// The above states will be recorded
RevDeBugAPI.Snapshot.RecordSnapshot("Recording");

How to add RevDeBug Trace ID to your logs

To use RevDeBug API in your ASP.NET Core project you need to add IRevDeBugTraceInfoAccessor to IOC of your controller or middleware and then simply call method GetTraceId()

Example ExceptionMiddleware.cs could look like this:

public class ExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger _logger;
        private readonly IRevDeBugTraceInfoAccessor _accessor;

        public ExceptionMiddleware(RequestDelegate next, ILogger logger, IRevDeBugTraceInfoAccessor accessor)
        {
            _logger = logger;
            _next = next;
            _accessor = accessor;
        }

        public async Task InvokeAsync(HttpContext httpContext)
        {
            try
            {
                await _next(httpContext);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Middleware exception error for path {Path}: {Message}, TraceID: {TraceId}",
                    httpContext.Request.Path, ex.Message, _accessor.GetTraceId());
                await HandleExceptionAsync(httpContext, ex);
            }
        }
        
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        { 
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            await context.Response.WriteAsync("Internal Server Error from the custom middleware.");
        }
    }

How to add custom tags to span in Trace

To add custom tagging to your trace span, you need to add the following changes to your endpoints, please replace TagName and TagValue with your custom tag:

using SkyApm.Tracing;

namespace Sample.Controllers
{
    private IEntrySegmentContextAccessor _entrySegmentContextAccessor;
    public SampleController(SampleContext sampleContext, IEntrySegmentContextAccessor entrySegmentContextAccessor)
    {
        // Your code here
        _entrySegmentContextAccessor = entrySegmentContextAccessor;
    }
    
    public SampleResult Edit(string id)
    {
        var segmentContext = _entrySegmentContextAccessor.Context;
        segmentContext.Span.AddTag("TagName", "TagValue");
        // Your code here
    }
}

Last updated