Customizing error reports

In order to quickly reproduce and fix errors, it is often helpful to send additional application-specific diagnostic data to Bugsnag.

Adding callbacks

Custom call back functions (middleware) can be configured to run just before an error report is sent. These callbacks have full access to the error report and can modify it before it’s sent. They also have the opportunity to prevent the error report from being sent all together. The middleware signature takes an error report and does not return anything. See the built in middleware for examples.

The Report object

The report object is a dictionary of the payload which will be serialized and sent to Bugsnag. The following convenience methods/properties exist on the object to allow you to manipulate and/or perform logic on in any middleware that you wish to write.

Configuration

Access the configuration object that the client that generated this report was initialized with.

bugsnag.BeforeNotify(report => {
  if (report.Configuration.AppVersion == "2")
  {
    // take some action on the report.
  }
});

Event

Access the event contained in this report.

bugsnag.BeforeNotify(report => {
  report.Event.Context = "an-important-context";
});

Ignore()

Calling Ignore() on a report object will cause the report to not be sent to Bugsnag. This means that you can choose dynamically not to send an error depending on application state or the error itself.

bugsnag.BeforeNotify(report => {
  report.Ignore();
});

Ignored

When this property is true, the event has been ignored and will not be sent to Bugsnag.

bugsnag.BeforeNotify(report => {
  if (report.Ignored)
  {
    // perform some action
  }
});

OriginalException

Access the original System.Exception instance that this report was generated from.

bugsnag.BeforeNotify(report => {
  if (report.OriginalException is System.NotImplementedException)
  {
    // perform some action
  }
});

The Event object

The event object is a dictionary section of the payload which will be serialized and sent to Bugsnag. The following convenience methods/properties exist on the object to allow you to manipulate and/or perform logic on in any middleware that you wish to write.

Context

Set the context of the error report. This is notionally the location of the error. Context is displayed in the dashboard prominently.

bugsnag.BeforeNotify(report => {
  report.Event.Context = "billing";
});

GroupingHash

Sets the grouping hash of the error report. All errors with the same grouping hash are grouped together. This is an advanced usage of the library and mis-using it will cause your errors not to group properly in your dashboard.

bugsnag.BeforeNotify(report => {
  report.Event.GroupingHash = report.OriginalException.Message;
});

IsHandled

Gets the handled status of this event. If the event is being sent due to being called with Notify then this will true, otherwise if it sent due to an unhandled exception handler then this will be false.

bugsnag.BeforeNotify(report => {
  if (report.Event.IsHandled)
  {
    // perform some action
  }
});

Metadata

Provides access to the metadata in the error report. You can also add your own additional metadata using this property.

bugsnag.BeforeNotify(report => {
  // add a single key under the 'custom' tab in the dashboard
  report.Event.Metadata.Add("paying account", true);

  // add a new tab into the dashboard with 'account' as the name
  report.Event.Metadata.Add("account", new Dictionary<string, object> { { "paying", true } });
});

Severity

Set the severity of the error. Severity can be error, warning or info.

bugsnag.BeforeNotify(report => {
  report.Event.Severity = Bugsnag.Severity.Info;
});

User

Set information about the user the error occurred for.

bugsnag.BeforeNotify(report => {
  report.Event.User = new Bugsnag.Payload.User {
    Id = "1",
    Name = "Testy McTest",
    Email = "support@bugsnag.com" };
});