Zed.GlobalErrorHandler 2.0.0

dotnet add package Zed.GlobalErrorHandler --version 2.0.0
                    
NuGet\Install-Package Zed.GlobalErrorHandler -Version 2.0.0
                    
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="Zed.GlobalErrorHandler" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zed.GlobalErrorHandler" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Zed.GlobalErrorHandler" />
                    
Project file
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 Zed.GlobalErrorHandler --version 2.0.0
                    
#r "nuget: Zed.GlobalErrorHandler, 2.0.0"
                    
#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 Zed.GlobalErrorHandler@2.0.0
                    
#: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=Zed.GlobalErrorHandler&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Zed.GlobalErrorHandler&version=2.0.0
                    
Install as a Cake Tool

Zed.GlobalErrorHandler

NuGet

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

  1. Install via NuGet:
dotnet add package Zed.GlobalErrorHandler
  1. 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.");
  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 121 7/17/2025
1.0.0 128 7/16/2025