计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
sentry【效率与协作】Use when adding error tracking, performance monitoring, and release health to .NET applications — ASP.NET Core, MAUI, WPF, Blazor, EF Core integration. Sentry .NET SDK: cross-platform error and performance monitoring with source map support, breadcrumbs, and alerting.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: sentry description: "Use when adding error tracking, performance monitoring, and release health to .NET applications — ASP.NET Core, MAUI, WPF, Blazor, EF Core integration. Sentry .NET SDK: cross-platform error and performance monitoring with source map support, breadcrumbs, and alerting." tags:
项目地址: https://github.com/getsentry/sentry-dotnet
官方文档: https://docs.sentry.io/platforms/dotnet/
NuGet: https://www.nuget.org/packages/Sentry
最新版本: Sentry / Sentry.AspNetCore 6.11.1(截至2026-09,NuGet)
许可证: MIT
Sentry 是一个开源的 错误追踪与性能监控平台,.NET SDK 支持将异常、崩溃、性能指标统一上报到 Sentry 服务端(SaaS 或自托管)。
| 特性 | 说明 |
|---|---|
| 自动异常捕获 | 集成 ASP.NET Core / MAUI / WPF / Blazor / Console |
| 性能监控 | Transaction + Span 模型,自动采集 HTTP/DB/EF Core 耗时 |
| 源码映射 | Release + Debug Files 支持,还原压缩/混淆后的堆栈 |
| Breadcrumb | 自动记录用户操作、HTTP 请求、日志等上下文 |
| Session Replay | 用户会话回放(Web 前端支持) |
| Alerting | 基于 Issue / Metric 的告警规则 |
| 自托管 | 完整的自托管部署方案(Docker/Kubernetes) |
| 包名 | 用途 |
|---|---|
Sentry | 核心 SDK(控制台/Worker) |
Sentry.AspNetCore | ASP.NET Core Web API / MVC |
Sentry.Maui | .NET MAUI 移动应用 |
Sentry.AspNetCore.Blazor.WebAssembly | Blazor WASM |
Sentry.EntityFramework | Entity Framework 6 集成(EF Core 查询经内置 DiagnosticSource 自动采集) |
Sentry.DiagnosticSource | DiagnosticSource 事件采集 |
Sentry.OpenTelemetry | OpenTelemetry 集成 |
Sentry.Profiling | 代码级性能剖析 |
# ASP.NET Core 项目
dotnet add package Sentry.AspNetCore
# 控制台 / Worker 项目
dotnet add package Sentry
# 可选集成
dotnet add package Sentry.EntityFramework # EF6(EF Core 无需此包)
dotnet add package Sentry.DiagnosticSource # HTTP / ADO.NET 自动 Span
dotnet add package Sentry.OpenTelemetry # OTel 桥接
dotnet add package Sentry.Profiling # 性能剖析
// ASP.NET Core (Program.cs)
builder.WebHost.UseSentry(o =>
{
o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
o.TracesSampleRate = 1.0; // 100% 性能追踪采样
o.Environment = builder.Environment.EnvironmentName;
o.Release = "my-app@1.0.0";
});
// 控制台应用
SentrySdk.Init(o =>
{
o.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
o.TracesSampleRate = 0.2; // 20% 采样
});
try
{
DoSomething();
}
catch (Exception ex)
{
// 添加额外上下文
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("feature", "payment");
scope.SetExtra("request_body", requestBody);
scope.User = new SentryUser { Id = "12345", Email = "user@example.com" };
});
SentrySdk.CaptureException(ex);
}
// 创建 Transaction
var transaction = SentrySdk.StartTransaction("process-order", "task");
SentrySdk.ConfigureScope(s => s.Transaction = transaction);
try
{
// Span: 数据库查询
var dbSpan = transaction.StartChildSpan("db.query", "SELECT * FROM orders");
var orders = await db.QueryAsync("SELECT * FROM orders WHERE ...");
dbSpan.Finish();
// Span: 业务逻辑
var bizSpan = transaction.StartChildSpan("business.process");
ProcessOrders(orders);
bizSpan.Finish();
transaction.Finish(SpanStatus.Ok);
}
catch (Exception ex)
{
transaction.Finish(ex);
}
// ASP.NET Core 自动记录:
// - HTTP 请求/响应
// - EF Core 查询
// - 日志消息(需配置 SentryLogs)
// - 认证事件
// 手动添加 Breadcrumb
SentrySdk.AddBreadcrumb(
"User clicked checkout button",
category: "ui",
level: BreadcrumbLevel.Info
);
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(o =>
{
o.Dsn = Environment.GetEnvironmentVariable("SENTRY_DSN");
o.TracesSampleRate = 0.3;
o.ProfilesSampleRate = 0.1; // 采样率
o.Environment = builder.Environment.EnvironmentName;
o.Release = quot;my-api@{Assembly.GetExecutingAssembly().GetName().Version}";
// 性能:只追踪特定路由
o.TracePropagationTargets = new[] { "https://my-api.com" };
});
// EF Core 查询 Span 由 SDK 内置 DiagnosticSource 集成自动采集;
// EF6 才需要额外安装 Sentry.EntityFramework
var app = builder.Build();
// 新版 SDK 中 ASP.NET Core 请求 Transaction 自动创建(TracesSampleRate > 0 即生效),
// 无需再手动调用 UseSentryTracing()
app.MapControllers();
app.Run();
# Sentry 自托管(开发环境)
git clone https://github.com/getsentry/self-hosted.git
cd self-hosted
./install.sh
# 访问 http://localhost:9000
TracesSampleRate = 0.1-0.3,避免高流量下性能影响Release 标签,让 Sentry 关联代码变更与错误SendDefaultPii = false(默认)| 问题 | 解决方案 |
|---|---|
| DSN 在哪里获取? | Sentry Dashboard → Settings → Projects → Client Keys |
| 如何过滤敏感数据? | 使用 BeforeSend 回调或 BeforeSendTransaction |
| ASP.NET Core 中性能追踪不生效? | 确保 TracesSampleRate > 0(新版 SDK 自动创建请求 Transaction,无需额外中间件) |
| 如何与 OpenTelemetry 共存? | 安装 Sentry.OpenTelemetry,通过 OTel 桥接 |
| EF Core 查询没有追踪? | 确认 ADO.NET/DiagnosticSource 集成未被禁用;Sentry.EntityFramework 仅用于 EF6 |
dotnet add package Sentry.AspNetCore(ASP.NET Core)或 Sentry(基础)appsettings.json 中设置 Sentry:Dsn 或环境变量 SENTRY_DSNbuilder.WebHost.UseSentry()(Web)或 SentrySdk.Init()(控制台),并设置 TracesSampleRateSentrySdk.CaptureMessage() 或 SentrySdk.CaptureException()TracesSampleRate > 0 才会生成 Transaction/Span;新版 SDK 自动插桩,旧文档中的 UseSentryTracing() 中间件已无需调用TracesSampleRate = 0.1(10%),避免过多数据BeforeSend 回调过滤敏感数据(PII、密码等)SENTRY_SECRET_KEY 和邮件服务