Zed.GlobalErrorHandler
2.0.0
dotnet add package Zed.GlobalErrorHandler --version 2.0.0
NuGet\Install-Package Zed.GlobalErrorHandler -Version 2.0.0
<PackageReference Include="Zed.GlobalErrorHandler" Version="2.0.0" />
<PackageVersion Include="Zed.GlobalErrorHandler" Version="2.0.0" />
<PackageReference Include="Zed.GlobalErrorHandler" />
paket add Zed.GlobalErrorHandler --version 2.0.0
#r "nuget: Zed.GlobalErrorHandler, 2.0.0"
#:package Zed.GlobalErrorHandler@2.0.0
#addin nuget:?package=Zed.GlobalErrorHandler&version=2.0.0
#tool nuget:?package=Zed.GlobalErrorHandler&version=2.0.0
Zed.GlobalErrorHandler
Zed.GlobalErrorHandler is a simple global exception handling middleware for ASP.NET Core. It allows you to map custom exceptions to specific HTTP status codes, log exceptions, and return consistent JSON error responses.
Features
- Centralized global exception handling
- Custom exception-to-status-code mapping
- Configurable default status code and error message
- Built-in logging support
- Minimal setup
Usage
- Install via NuGet:
dotnet add package Zed.GlobalErrorHandler
- Register in
Program.cs
builder.Services.AddGlobalErrorHandler(opt =>
{
// Add your project exceptions and status codes
opt.Add(typeof(UnauthorizedAccessException), StatusCodes.Status401Unauthorized);
opt.Add(typeof(NotFoundException), StatusCodes.Status404NotFound);
},
// If you want to override the default status code and error message (used for unregistered exceptions):
defaultStatusCode: 400,
defaultErrorMessage: "Custom default error message.");
- Add global error handler to the pipeline
app.UseGlobalErrorHandler();
How It Works
Internally, the middleware uses the provided mapping to determine the correct HTTP status code for an exception. If the exception type is not mapped, it uses the default status code and message.
{
"error": "Your exception message or default error message.",
"statusCode": 400
}
Logged errors include exception details and stack trace.
Advanced logging with Serilog
You can also enable logging using Serilog with support for both rolling file logs and Seq server:
builder.Services.AddGlobalErrorHandler(opt =>
{
// Register your exceptions and HTTP status codes
})
.AddLogging(logging =>
{
logging.AddSerilogGlobalLogger(options =>
{
// Enable file logging (daily rolling)
options.WithFile("logs/log-.txt", RollingInterval.Day, 7);
// Enable Seq logging (make sure Seq is running at the given URL)
options.WithSeq("http://localhost:5341");
// Optional: Set application name and minimum log level
options.WithApplicationName("YourProjectName");
options.WithMinimumLevel(LogEventLevel.Information);
});
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Serilog.AspNetCore (>= 8.0.3)
- Serilog.Sinks.Console (>= 6.0.0)
- Serilog.Sinks.File (>= 6.0.0)
- Serilog.Sinks.Seq (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.