Skip to content

Commit

Permalink
feat: NotifyAsync and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 committed Jan 22, 2023
1 parent 786b4c5 commit 216aad1
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 46 deletions.
22 changes: 22 additions & 0 deletions MIT_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2023, Honeybadger Industries LLC

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
83 changes: 61 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ This is the .Net Honeybadger Notifier.

## Getting Started

Clone/Fork. Since the project is currently at MVP stage and not yet published on nuget.org, the best way to use it would be to
clone or fork it and build as a local project in your solution.

### For .Net Core Web App

1. Reference the `Honeybadger.DotNetCoreWebApp` project.
1. Install Honeybadger.DotNetCore from Nuget
```
dotnet add package Honeybadger.DotNetCore
```
2. Register the _Honeybadger Middleware_:
```c#
var builder = WebApplication.CreateBuilder(args);
Expand All @@ -34,7 +34,7 @@ app.MapGet("/", ([FromServices] IHoneybadgerClient client) =>
return "Hello World!";
});
```
3. Any unhandled exceptions should be reported to Honeybadger automatically:
2. Any unhandled exceptions should be reported to Honeybadger automatically:
```c#
app.MapGet("/debug", () =>
{
Expand All @@ -46,7 +46,10 @@ See example project in `examples/Honeybadger.DotNetCoreWebApp`.

### As a custom logging provider

1. Reference the `Honeybadger.Extensions.Logging` project.
1. Install Honeybadger.Extensions.Logging from Nuget
```
dotnet add package Honeybadger.Extensions.Logging
```
2. Register the custom logging provider:
```c#
var builder = WebApplication.CreateBuilder(args);
Expand All @@ -64,12 +67,15 @@ app.MapGet("/notify", ([FromServices] ILogger logger) =>

See example project in `examples/Honeybadger.DotNetCoreWebApp.Logger`.

### Using the client manually
### Using the SDK manually

1. Reference the `Honeybadger` project.
1. Install the [Honeybadger Nuget](https://www.nuget.org/packages/Honeybadger).
```
dotnet add package Honeybadger
```
2. Initialize the _Honeybadger Client_:
```c#
var client = HoneybadgerSdk.Init(new HoneybadgerOptions
var client = HoneybadgerSdk.Init(new HoneybadgerOptions("apiKey")
{
AppEnvironment = "development"
});
Expand All @@ -81,17 +87,50 @@ client.Notify("hello from .Net !");

See example project in `examples/Honeybadger.Console`.

## Changelog

Changelog is automatically generated using [Conventional Commits](https://www.conventionalcommits.org/) with [versionize](https://github.com/versionize/versionize).
Conventional Commits are enforced with a pre-commit git hook (using [husky](https://alirezanet.github.io/Husky.Net/guide/)).

## 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](https://github.com/honeybadger-io/honeybadger-dotnet/pulls)
7. Make sure that CI checks are passing

## Releasing

All packages are published on nuget.org with a [Github Actions Worfklow](./.github/workflows/release.yml).
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)._

## Known bugs

- [ ] Always shows "Honeybadger.HoneybadgerClient" when reporting a notice with a message

## TODO
- [ ] Publish _.Net Core_ Nuget to nuget.org
- [ ] Publish _Honeybadger Logging Provider_ Nuget to nuget.org
- [x] Target .Net 5.0
- [x] Target .Net Core 3.1
- [x] Target .Net Standard 2.1
- [x] Target .Net Standard 2.0
- [ ] Support more features of the [Honeybadger Client Library Spec](https://www.notion.so/honeybadger/Client-Library-Spec-aa891332f7874196aa0695b6d38dca66). Tracked [here](https://pc-hiteq-software.monday.com/boards/1981156519/).

### References
- Create a [class library](https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli) project
- StackTrack class
- [Honeybadger Client Library Spec](https://www.notion.so/honeybadger/Client-Library-Spec-aa891332f7874196aa0695b6d38dca66)
- [Honeybadger Crystal](https://github.com/honeybadger-io/honeybadger-crystal) - Recently Released

- [ ] Publish README with basic info to setup core nuget
- [ ] Publish Honeybadger.DotNetCore with README
- [ ] Publish Honeybadger.Extensions.Logging with REDME
- [ ] Deploy under Honeybadger org
- [ ] Implement Error Grouping (custom fingerprint)
- [ ] Implement Error Tags
- [ ] Allow excluding errors (either with a BeforeNotify method or exception classes config)
- [ ] Implement Filter Keys (exclude sensitive keys)
- [ ] Implement Checkins
- [ ] Implement Collect User Feedback
- [ ] Create guide for Deployment Tracking
- [ ] Create integration guide in honeybadger-docs project

## License

This Honeybadger repository and published packages are MIT licensed. See the [MIT-LICENSE](./MIT_LICENSE) file in this repository for details.
6 changes: 2 additions & 4 deletions examples/Honeybadger.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

Console.WriteLine("Hello, World!");

var client = HoneybadgerSdk.Init(new HoneybadgerOptions
var client = HoneybadgerSdk.Init(new HoneybadgerOptions("YOUR_HONEYBADGER_API_KEY")
{
AppEnvironment = "development"
});

client.Notify("hello from .Net !");

Thread.Sleep(1000);
client.Notify("hello from .Net !");
5 changes: 4 additions & 1 deletion examples/Honeybadger.DotNetCoreWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHoneybadger();
builder.Services.AddHoneybadger(new HoneybadgerOptions("YOUR_HONEYBADGER_API_KEY")
{
AppEnvironment = "development"
});

var app = builder.Build();
app.MapGet("/", ([FromServices] IHoneybadgerClient client) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7260;http://localhost:5177",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "development",
"HONEYBADGER_API_KEY": "hbp_besml32bFCbMcIsL8QDgGeLqivUuD3472J38"
}
"applicationUrl": "https://localhost:7260;http://localhost:5177"
},
"IIS Express": {
"commandName": "IISExpress",
Expand Down
12 changes: 2 additions & 10 deletions src/Honeybadger.DotNetCore/HoneybadgerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ public HoneybadgerMiddleware(RequestDelegate requestDelegate)

public async Task Invoke(HttpContext context, IHoneybadgerClient client)
{
// needed for HoneybadgerStartupFilter.cs
context.Items[HttpContextItemsKey] = client;

// capture the request information now as the http context
// may be changed by other error handlers after an exception
// has occurred
// var bugsnagRequestInformation = context.ToRequest();

// client.BeforeNotify(report => {
// report.Event.Request = bugsnagRequestInformation;
// });

if (!client.Options.ReportData)
{
await _next(context);
Expand All @@ -38,7 +30,7 @@ public async Task Invoke(HttpContext context, IHoneybadgerClient client)
}
catch (Exception exception)
{
client.Notify(exception);
await client.NotifyAsync(exception);
throw;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Honeybadger/Honeybadger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>Honeybadger client for .Net</Description>
<RepositoryUrl>https://github.com/honeybadger-io/honeybadger-dotnet</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseUrl>https://github.com/honeybadger-io/honeybadger-dotnet/MIT_LICENSE</PackageLicenseUrl>
<PackageTags>honeybadger, exception, error, monitoring</PackageTags>
</PropertyGroup>
<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="/" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IsExternalInit" Version="1.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
40 changes: 36 additions & 4 deletions src/Honeybadger/HoneybadgerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Notify(string message)
public void Notify(string message, Dictionary<string, object> context)
{
var notice = NoticeFactory.Make(this, message, GetContext(context));
Send(notice);
Notify(notice);
}

public void Notify(Exception error)
Expand All @@ -44,7 +44,39 @@ public void Notify(Exception error)
public void Notify(Exception error, Dictionary<string, object> context)
{
var notice = NoticeFactory.Make(this, error, GetContext(context));
Send(notice);
Notify(notice);
}

private void Notify(Notice notice)
{
Send(notice).Wait();
}

public Task NotifyAsync(string message)
{
return NotifyAsync(message, new Dictionary<string, object>());
}

public Task NotifyAsync(string message, Dictionary<string, object> context)
{
var notice = NoticeFactory.Make(this, message, GetContext(context));
return NotifyAsync(notice);
}

public Task NotifyAsync(Exception error)
{
return NotifyAsync(error, new Dictionary<string, object>());
}

public Task NotifyAsync(Exception error, Dictionary<string, object> context)
{
var notice = NoticeFactory.Make(this, error, GetContext(context));
return NotifyAsync(notice);
}

private Task NotifyAsync(Notice notice)
{
return Send(notice);
}

public void AddContext(Dictionary<string, object> context)
Expand Down Expand Up @@ -121,7 +153,7 @@ private Dictionary<string, object> GetContext(Dictionary<string, object>? contex
return Options.BreadcrumbsEnabled ? _breadcrumbs.Value?.ToArray() : null;
}

private async void Send(Notice notice)
private async Task Send(Notice notice)
{
Console.WriteLine("Ready to send report to Honeybadger");
var request = new HttpRequestMessage(HttpMethod.Post, "v1/notices");
Expand All @@ -145,7 +177,7 @@ private async void Send(Notice notice)
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
await Console.Error.WriteLineAsync(ex.Message);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Honeybadger/IHoneybadgerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public interface IHoneybadgerClient
public void Notify(string message, Dictionary<string, object> context);
public void Notify(Exception error);
public void Notify(Exception error, Dictionary<string, object> context);
public Task NotifyAsync(string message);
public Task NotifyAsync(string message, Dictionary<string, object> context);
public Task NotifyAsync(Exception error);
public Task NotifyAsync(Exception error, Dictionary<string, object> context);
public void AddContext(Dictionary<string, object> context);
public void ResetContext();
public void AddBreadcrumb(string message, string category, Dictionary<string, object?>? options = null);
Expand Down

0 comments on commit 216aad1

Please # to comment.