Skip to main content

Error Monitoring Quick Start

Quick setup guide for Sentry error monitoring with AI integration.

Quick Setup (30 minutes)

1. Create Sentry Account

  1. Go to sentry.io
  2. Sign up for free account
  3. Create new project:
    • Platform: ASP.NET Core
    • Project name: FishingLog.API

2. Install Sentry SDK

cd FishingLog.API
dotnet add package Sentry.AspNetCore
dotnet add package Sentry.EntityFramework

3. Configure Sentry

Update Program.cs:

using Sentry;

var builder = WebApplication.CreateBuilder(args);

// Add Sentry BEFORE other services
builder.WebHost.UseSentry(options =>
{
options.Dsn = builder.Configuration["Sentry:Dsn"];
options.Environment = builder.Environment.EnvironmentName;
options.TracesSampleRate = builder.Environment.IsProduction() ? 0.1 : 1.0;
options.SendDefaultPii = false;

// Add custom tags
options.BeforeSend = (sentryEvent) =>
{
sentryEvent.SetTag("service", "api");
sentryEvent.SetTag("version", "1.0.0");
return sentryEvent;
};
});

// Add Sentry to logging
builder.Logging.AddSentry();

// ... rest of your code

var app = builder.Build();

// Add Sentry middleware
app.UseSentryTracing();

// ... rest of your code

Update appsettings.json:

{
"Sentry": {
"Dsn": "https://YOUR_DSN@sentry.io/PROJECT_ID",
"Environment": "Development"
}
}

Update appsettings.Production.json:

{
"Sentry": {
"Dsn": "https://YOUR_DSN@sentry.io/PROJECT_ID",
"Environment": "Production",
"TracesSampleRate": 0.1
}
}

4. Test It Works

Add a test endpoint:

[ApiController]
[Route("api/[controller]")]
public class TestErrorController : ControllerBase
{
[HttpGet("throw")]
public IActionResult ThrowError()
{
throw new Exception("Test error for Sentry");
}
}

Call GET /api/testerror/throw and check Sentry dashboard.

AI Integration Setup

1. Create GitHub Action

Create .github/workflows/sentry-auto-fix.yml:

name: Sentry Auto-Fix

on:
repository_dispatch:
types: [sentry-error]

jobs:
auto-fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '9.0.x'

- name: Analyze Error
id: analyze
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
# Your AI analysis script here
echo "error_summary=NullReferenceException" >> $GITHUB_OUTPUT
echo "confidence=85" >> $GITHUB_OUTPUT

- name: Create Issue
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `[Auto] ${process.env.ERROR_SUMMARY}`,
body: 'Auto-generated from Sentry error',
labels: ['bug', 'auto-generated']
});

2. Configure Sentry Webhook

  1. Go to Sentry → Settings → Integrations
  2. Add Webhook integration
  3. URL: https://api.github.com/repos/YOUR_ORG/YOUR_REPO/dispatches
  4. Secret: Generate and save
  5. Select events: error.created

3. Set Up Secrets

Add to GitHub Secrets:

  • OPENAI_API_KEY - Your OpenAI API key
  • SENTRY_WEBHOOK_SECRET - Sentry webhook secret

Basic AI Error Analysis Script

Create scripts/analyze-error.js:

const { OpenAI } = require('openai');
const fs = require('fs');

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});

async function analyzeError(errorData) {
const prompt = `
Analyze this error and suggest a fix:

Error: ${errorData.message}
Stack Trace: ${errorData.stackTrace}
File: ${errorData.file}
Line: ${errorData.line}

Return JSON:
{
"error_summary": "brief summary",
"root_cause": "explanation",
"suggested_fix": "detailed fix",
"confidence": 85
}
`;

const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: 'You are an expert .NET developer.' },
{ role: 'user', content: prompt }
],
response_format: { type: 'json_object' }
});

return JSON.parse(response.choices[0].message.content);
}

// Usage
const errorData = JSON.parse(process.argv[2]);
analyzeError(errorData).then(result => {
console.log(JSON.stringify(result));
});

Testing the Flow

  1. Trigger an error in your app
  2. Check Sentry - Error should appear
  3. Webhook fires - Check GitHub Actions logs
  4. Issue created - Check GitHub Issues
  5. AI analyzes - Check analysis output

Next Steps

  1. ✅ Basic Sentry setup complete
  2. ⬜ Add more context to errors (user info, request data)
  3. ⬜ Set up alert rules in Sentry
  4. ⬜ Build full AI analysis service
  5. ⬜ Add code fix generation
  6. ⬜ Add PR creation
  7. ⬜ Monitor and refine

See Error Monitoring Strategy for complete details.