Error Monitoring Quick Start
Quick setup guide for Sentry error monitoring with AI integration.
Quick Setup (30 minutes)
1. Create Sentry Account
- Go to sentry.io
- Sign up for free account
- 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
- Go to Sentry → Settings → Integrations
- Add Webhook integration
- URL:
https://api.github.com/repos/YOUR_ORG/YOUR_REPO/dispatches - Secret: Generate and save
- Select events:
error.created
3. Set Up Secrets
Add to GitHub Secrets:
OPENAI_API_KEY- Your OpenAI API keySENTRY_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
- Trigger an error in your app
- Check Sentry - Error should appear
- Webhook fires - Check GitHub Actions logs
- Issue created - Check GitHub Issues
- AI analyzes - Check analysis output
Next Steps
- ✅ Basic Sentry setup complete
- ⬜ Add more context to errors (user info, request data)
- ⬜ Set up alert rules in Sentry
- ⬜ Build full AI analysis service
- ⬜ Add code fix generation
- ⬜ Add PR creation
- ⬜ Monitor and refine
See Error Monitoring Strategy for complete details.