OASSO.ErrorReporter
1.0.1
dotnet add package OASSO.ErrorReporter --version 1.0.1
NuGet\Install-Package OASSO.ErrorReporter -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OASSO.ErrorReporter" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OASSO.ErrorReporter" Version="1.0.1" />
<PackageReference Include="OASSO.ErrorReporter" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OASSO.ErrorReporter --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OASSO.ErrorReporter, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OASSO.ErrorReporter@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OASSO.ErrorReporter&version=1.0.1
#tool nuget:?package=OASSO.ErrorReporter&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OASSO.ErrorReporter
一个轻量级、无阻塞的 .NET Standard 2.0 错误上报客户端。专为公司内部项目设计,支持异步上报、自定义错误级别、超时控制以及异常自动捕获,且保证上报失败绝不影响主业务逻辑。
📦 安装
通过 .NET CLI 安装:
dotnet add package OASSO.ErrorReporter
或者通过 Visual Studio 的 NuGet 包管理器搜索 OASSO.ErrorReporter 进行安装。
🚀 快速开始
1. 初始化配置
在应用程序启动时(例如 Program.cs 或 Startup.cs 中),调用 Init 方法进行全局初始化。此操作只需执行一次。
using OASSO.ErrorReporter;
ErrorReporter.Init(new ErrorReportOptions
{
BaseUrl = "http://your-internal-server:port", // 必填:内部服务地址
Project = "YourProject", // 必填:项目名称
DefaultRecipient = "user1,user2", // 可选:默认接收者(多个用逗号分隔)
Enabled = true, // 可选:是否启用上报,默认 true
TimeoutSeconds = 5 // 可选:请求超时时间(秒),默认 5 秒
});
2. 上报错误
支持多种错误上报方式,按需选择:
方式 A:直接上报异常对象(最推荐)
try
{
// 你的业务代码
}
catch (Exception ex)
{
await ErrorReporter.ReportAsync(ex, url: "https://your-app-url/error");
}
方式 B:上报自定义错误消息
await ErrorReporter.ReportAsync(
message: "用户登录失败",
detail: "账号不存在或密码错误",
level: ErrorLevel.Warning,
url: "/login",
recipient: "admin"
);
方式 C:同步上报(不推荐,仅用于无法使用 async 的场景)
ErrorReporter.Report("系统发生严重错误", level: ErrorLevel.Critical);
3. 使用扩展方法自动捕获(简化代码)
如果你不想到处写 try-catch,可以使用 ErrorReporterExtensions 提供的 TryExecuteAsync 扩展方法,异常会被自动捕获并上报:
public async Task DoSomethingAsync()
{
bool success = await this.TryExecuteAsync(async () =>
{
// 你的业务逻辑
await Task.Delay(100);
throw new Exception("业务处理失败");
}, url: "/api/do-something");
if (!success)
{
// 处理失败逻辑(异常已自动上报)
}
}
⚙️ 配置项说明 (ErrorReportOptions)
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| BaseUrl | string |
(必填) | 内部服务地址,如 http://server:port |
| Project | string |
(必填) | 项目名称 |
| DefaultRecipient | string |
null |
默认接收者(多个用逗号分隔) |
| Enabled | bool |
true |
是否启用上报。设为 false 可全局关闭 |
| TimeoutSeconds | int |
5 |
HTTP 请求超时时间(秒) |
📊 错误级别 (ErrorLevel)
| 枚举值 | 说明 |
|---|---|
Warning |
警告,一般不影响业务流程 |
Error |
一般错误,默认级别 |
Critical |
严重错误,需立即处理 |
🛡️ 设计原则
- 静默失败:如果上报过程中出现任何网络异常或未初始化,库内部会静默捕获,绝不影响主业务逻辑。
- 异步优先:全部基于标准异步实现,不阻塞调用线程。
- 轻量依赖:无沉重的第三方依赖,保持包体积小巧。
📄 许可证
本项目基于 MIT 许可证开源。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.1 版本:优化图标显示,修复部分配置项缺失的问题