dotnet reference: Documentation for the Honeybadger .NET/C# client library (SDK) and platform.
# Honeybadger for .NET
> Honeybadger monitors your .NET applications for errors and exceptions so that you can fix them wicked fast.
**Typical installation time:** \~5 minutes Hi there! You’ve found Honeybadger’s guide to **.NET exception and error tracking**. Once installed, Honeybadger will automatically report errors from your .NET or C# application. ## Getting started [Section titled “Getting started”](#getting-started) [Source Code](https://github.com/honeybadger-io/honeybadger-dotnet) ### Configuration [Section titled “Configuration”](#configuration) The Honeybadger Notifier can be configured using the `HoneybadgerOptions` class. Honeybadger can be configured by passing the options when registering the service, or through your `appsettings.json` file. Honeybadger will attempt to automatically figure out the `ProjectRoot` directory, which should be the root of your project or solution. A valid `ProjectRoot` directory will allow Honeybadger to classify stack frames as either *application* code or *all* other code (e.g. framework code) and hence provide better error reports. See below for examples on how to configure Honeybadger for different types of applications. ### For .NET Core web app [Section titled “For .NET Core web app”](#for-net-core-web-app) #### 1. Install Honeybadger.DotNetCore from NuGet [Section titled “1. Install Honeybadger.DotNetCore from NuGet”](#1-install-honeybadgerdotnetcore-from-nuget)
```sh
dotnet add package Honeybadger.DotNetCore
```
#### 2. Register the Honeybadger middleware: [Section titled “2. Register the Honeybadger middleware:”](#2-register-the-honeybadger-middleware)
```c#
var builder = WebApplication.CreateBuilder(args);
builder.AddHoneybadger(configure =>
{
configure.ApiKey = "PROJECT_API_KEY";
});
```
Or you can configure Honeybadger through your `appsettings.json` file, by adding a `Honeybadger` section:
```json
{
"Honeybadger": {
"ApiKey": "PROJECT_API_KEY",
"AppEnvironment": "Development",
"ReportData": true
}
}
```
And simply call `AddHoneybadger` without any parameters:
```c#
var builder = WebApplication.CreateBuilder(args);
builder.AddHoneybadger();
```
#### Usage [Section titled “Usage”](#usage) You can access the *Honeybadger Client* using *DI*:
```c#
app.MapGet("/", ([FromServices] IHoneybadgerClient honeybadger) =>
{
honeybadger.AddBreadcrumb("reached index route", "route", new Dictionary());
return "Hello World!";
});
```
Any unhandled exceptions should be reported to Honeybadger automatically (unless `ReportUnhandledExceptions` is set to `false`):
```c#
app.MapGet("/debug", () =>
{
throw new Exception("hello from .Net Core Web App!");
});
```
See example project in `examples/Honeybadger.DotNetCoreWebApp`. ### As a custom logging provider [Section titled “As a custom logging provider”](#as-a-custom-logging-provider) #### 1. Install Honeybadger.Extensions.Logging from Nuget [Section titled “1. Install Honeybadger.Extensions.Logging from Nuget”](#1-install-honeybadgerextensionslogging-from-nuget)
```sh
dotnet add package Honeybadger.Extensions.Logging
```
#### 2. Register Honeybadger and additionally the custom logging provider: [Section titled “2. Register Honeybadger and additionally the custom logging provider:”](#2-register-honeybadger-and-additionally-the-custom-logging-provider)
```c#
var builder = WebApplication.CreateBuilder(args);
// or set the configuration in the appsettings.json file
builder.AddHoneybadger(configure =>
{
configure.ApiKey = "PROJECT_API_KEY";
});
builder.Logging.AddHoneybadger();
```
You should also configure the minimum log level as you would configure other log providers in .Net Core. The following would report only logged errors:
```json
{
"Logging": {
"Honeybadger": {
"Default": "Error"
}
}
}
```
And simply call `AddHoneybadger` and `Logging.AddHoneybadger` without any parameters:
```c#
var builder = WebApplication.CreateBuilder(args);
builder.AddHoneybadger();
builder.Logging.AddHoneybadger();
```
#### Usage [Section titled “Usage”](#usage-1) Errors from the `logger` will be reported to Honeybadger:
```c#
app.MapGet("/notify", ([FromServices] ILogger logger) =>
{
logger.LogError("hello from Honeybadger.Logger!");
return "Log reported to Honeybadger. Check your dashboard!";
});
```
See example project in `examples/Honeybadger.DotNetCoreWebApp.Logger`. ### Send a test notification [Section titled “Send a test notification”](#send-a-test-notification) You can send a test notification to Honeybadger to verify that the configuration is working. Add the following to your `Program.cs` file:
```c#
// ...
builder.AddHoneybadger();
// ...
var app = builder.Build();
var honeybadger = app.Services.GetRequiredService();
await honeybadger.NotifyAsync("Hello from .Net!");
```
Run the app. If the configuration is correctly set, you should see the notification in your Honeybadger dashboard. ### Automatic error reporting [Section titled “Automatic error reporting”](#automatic-error-reporting) Automatic error reporting is enabled by default, but you can disable it by setting the `ReportUnhandledExceptions` property to `false` in `HoneybadgerOptions`:
```json
{
"Honeybadger": {
"ApiKey": "PROJECT_API_KEY",
"AppEnvironment": "Development",
"ReportData": true,
"ReportUnhandledExceptions": false
}
}
```
### Using the SDK manually [Section titled “Using the SDK manually”](#using-the-sdk-manually) #### 1. Install the [Honeybadger Nuget](https://www.nuget.org/packages/Honeybadger). [Section titled “1. Install the Honeybadger Nuget.”](#1-install-the-honeybadger-nuget)
```sh
dotnet add package Honeybadger
```
#### 2. Initialize the Honeybadger client: [Section titled “2. Initialize the Honeybadger client:”](#2-initialize-the-honeybadger-client)
```c#
using Microsoft.Extensions.Options;
var options = new HoneybadgerOptions("PROJECT_API_KEY");
var honeybadger = new HoneybadgerClient(Options.Create(options));
```
#### 3. Call `notify` to report to Honeybadger: [Section titled “3. Call notify to report to Honeybadger:”](#3-call-notify-to-report-to-honeybadger)
```c#
// fire and forget
honeybadger.Notify("hello from .Net !");
// or async
await honeybadger.NotifyAsync("hello from .Net !");
```
See example project in `examples/Honeybadger.Console`. ## Supported .NET versions [Section titled “Supported .NET versions”](#supported-net-versions) All modern .Net Core applications are supported, up to .Net 9.0.