# 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:

```csharp
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.

## R**ecordings 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.

```csharp
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.

```csharp
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:

```csharp
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:

```csharp
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
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://revdebug.gitbook.io/revdebug/supported-langauges/c/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
