Skip to content

honeybadger-io/honeybadger-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Honeybadger for .Net Apps

This is the .Net Honeybadger Notifier.

Supported .Net versions:

All modern .Net Core applications are supported, up to .Net 9.0.

Getting Started

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

1. Install Honeybadger.DotNetCore from Nuget

dotnet add package Honeybadger.DotNetCore

2. Register the Honeybadger Middleware:

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:

{
   "Honeybadger": {
      "ApiKey": "{{PROJECT_API_KEY}}",
      "AppEnvironment": "Development",
      "ReportData": true
   }
}

Note: You should probably set your API key through environment variables or use the Secrets Manager, instead of hardcoding it in the appsettings.json file. You can read the official documentation for more information on how to do that in a .Net Core app.

And simply call AddHoneybadger without any parameters:

var builder = WebApplication.CreateBuilder(args);
builder.AddHoneybadger();

Usage

You can access the Honeybadger Client using DI:

app.MapGet("/", ([FromServices] IHoneybadgerClient honeybadger) =>
{
    honeybadger.AddBreadcrumb("reached index route", "route", new Dictionary<string, object?>());

    return "Hello World!";
});

Any unhandled exceptions should be reported to Honeybadger automatically (unless ReportUnhandledExceptions is set to false):

app.MapGet("/debug", () =>
{
    throw new Exception("hello from .Net Core Web App!");
});

See example project in examples/Honeybadger.DotNetCoreWebApp.

As a custom logging provider

1. Install Honeybadger.Extensions.Logging from Nuget

dotnet add package Honeybadger.Extensions.Logging

2. Register Honeybadger and additionally the custom logging provider:

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:

{
   "Logging": {
      "Honeybadger": {
         "Default": "Error"
      }
   }
}

And simply call AddHoneybadger and Logging.AddHoneybadger without any parameters:

var builder = WebApplication.CreateBuilder(args);
builder.AddHoneybadger();
builder.Logging.AddHoneybadger();

Usage

Errors from the logger will be reported to Honeybadger:

app.MapGet("/notify", ([FromServices] ILogger<Program> 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

Note: Honeybadger, by default, will not report errors in development environments. You can override the development environments by setting the DevelopmentEnvironments property in the options. Alternatively, you can set the ReportData property to true to report errors in all environments.

You can send a test notification to Honeybadger to verify that the configuration is working. Add the following to your Program.cs file:

// ...
builder.AddHoneybadger();
// ...
var app = builder.Build();
var honeybadger = app.Services.GetRequiredService<IHoneybadgerClient>();
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

Automatic error reporting is enabled by default, but you can disable it by setting the ReportUnhandledExceptions property to false in HoneybadgerOptions:

{
   "Honeybadger": {
      "ApiKey": "{{PROJECT_API_KEY}}",
      "AppEnvironment": "Development",
      "ReportData": true,
      "ReportUnhandledExceptions": false
   }
}

Using the SDK manually

1. Install the Honeybadger Nuget.

dotnet add package Honeybadger

2. Initialize the Honeybadger Client:

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:

// fire and forget
honeybadger.Notify("hello from .Net !");
// or async
await honeybadger.NotifyAsync("hello from .Net !");

See example project in examples/Honeybadger.Console.

Changelog

Changelog is automatically generated using Conventional Commits with versionize. Conventional Commits are enforced with a pre-commit git hook (using husky).

Contributing

  1. Fork the repo.
  2. Create a topic branch git checkout -b my_branch
  3. Commit your changes git commit -am "chore: boom"
  4. Write a test that verifies your changes
  5. Push to your branch git push origin my_branch
  6. Send a pull request
  7. Make sure that CI checks are passing

Releasing

Note: Automated releases are not yet fully functional. See Manual Releases for the current process.

All packages are published on nuget.org with a Github Actions Worfklow. The workflow does the following:

  • dotnet versionize - bump versions and generate changelog
  • dotnet pack
  • dotnet package push

Note: only users with write permissions can trigger this workflow (i.e. Collaborators).

Manual Releases

Our automated release process is not yet fully functional for the following reason: Since Honeybadger is a dependency of Honeybadger.Extensions.Logging and Honeybadger.DotNetCore, we need to release Honeybadger first, then update the dependencies in the other projects and finally release those two projects.

To release manually, execute the following steps:

  1. Run dotnet versionize in the root directory. This will bump the version in all projects and commit the new version as a tag.
  2. Run dotnet pack ./src/Honeybadger --configuration Release
  3. Run dotnet nuget push ./src/Honeybadger/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key YOUR_API_KEY
  4. Update the dependencies in Honeybadger.Extensions.Logging and Honeybadger.DotNetCore to the new version of Honeybadger.
  5. Run dotnet pack ./src/Honeybadger.Extensions.Logging --configuration Release
  6. Run dotnet nuget push ./src/Honeybadger.Extensions.Logging/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key YOUR_API_KEY
  7. Run dotnet pack ./src/Honeybadger.DotNetCore --configuration Release
  8. Run dotnet nuget push ./src/Honeybadger.DotNetCore/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key YOUR_API_KEY
  9. Commit the changes in the Honeybadger.Extensions.Logging and Honeybadger.DotNetCore projects.
  10. Push the changes to the repository - at this point you will have pushed the git tags + the new versions of the packages to nuget.org.

License

This Honeybadger repository and published packages are MIT licensed. See the MIT-LICENSE file in this repository for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •