C#

.NET Integration

Learn how to integrate ShitCode error tracking into your ASP.NET Core and .NET console applications.

ASP.NET Core Integration

The best way to integrate with ASP.NET Core is to create a dedicated service and middleware to capture unhandled exceptions globally.

1. Create Error Logging Service

First, create a service that handles the HTTP communication with our API.

ErrorLogService.cs
using System.Net.Http.Json;

public class ErrorLogService
{
    private readonly HttpClient _httpClient;
    private const string ApiKey = "YOUR_API_KEY";
    private const string SecurityKey = "YOUR_SECURITY_KEY";
    private const string WebhookUrl = $"https://your-domain.com/api/ingest/{ApiKey}";

    public ErrorLogService(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient();
        _httpClient.DefaultRequestHeaders.Add("X-Security-Key", SecurityKey);
    }

    public async Task LogErrorAsync(Exception exception)
    {
        var errorData = new
        {
            message = exception.Message,
            stackTrace = exception.StackTrace
        };

        try
        {
            await _httpClient.PostAsJsonAsync(WebhookUrl, errorData);
        }
        catch
        {
            // Silently fail - don't let error logging crash your app
        }
    }
}

2. Register the Service

Register the service and HttpClient in your Program.cs.

Program.cs
builder.Services.AddHttpClient();
builder.Services.AddSingleton<ErrorLogService>();

3. Global Exception Handler

Create middleware to catch all unhandled exceptions in your request pipeline.

ErrorLoggingMiddleware.cs
public class ErrorLoggingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ErrorLogService _errorLogService;

    public ErrorLoggingMiddleware(RequestDelegate next, ErrorLogService errorLogService)
    {
        _next = next;
        _errorLogService = errorLogService;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        try
        {
            await _next(context);
        }
        catch (Exception ex)
        {
            await _errorLogService.LogErrorAsync(ex);
            throw; // Re-throw to let other handlers process it
        }
    }
}

// Don't forget to use it in Program.cs
app.UseMiddleware<ErrorLoggingMiddleware>();

Security Warning: Never hard-code your API keys in production code. Use appsettings.json, User Secrets, or Environment Variables.

Console Application

For simple console applications or background services, you can use the API directly with HttpClient.

Program.cs
using System.Net.Http.Json;

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-Security-Key", "YOUR_SECURITY_KEY");

try
{
    // Your code that might fail
    throw new Exception("Something went wrong!");
}
catch (Exception ex)
{
    await httpClient.PostAsJsonAsync(
        "https://your-domain.com/api/ingest/YOUR_API_KEY",
        new { message = ex.Message, stackTrace = ex.StackTrace }
    );
}