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.

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

Event

Access the event contained in this report.

Request.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.

Request.Bugsnag().BeforeNotify(report => {
  report.Ignore();
});

Ignored

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

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

OriginalException

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

Request.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.

App

Application specific information.

Request.Bugsnag().BeforeNotify(report => {
  report.Event.App.Remove("version");
});

The list of breadrcumbs leading up to the current error.

Request.Bugsnag().BeforeNotify(report => {
  foreach (var breadcrumb in report.Event.Breadcrumbs)
  {
    breadcrumb["name"] = "[Filtered]";
  }

Context

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

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

Device

Information on the device where the error occurred. This object is a dictionary with some preset keys that can be manipulated.

Request.Bugsnag().BeforeNotify(report => {
  report.Event.Device.Remove("hostname");
});

Exceptions

Each exception that makes up the error that occurred. Inner exceptions, AggregateExceptions and ReflectionTypeLoadExceptions are all unwound and included in this list.

Each Exception is a is a dictionary including a stacktrace property, which is an array of dictionaries containing file, lineNumber, method and inProject.

Request.Bugsnag().BeforeNotify(report => {
  foreach (var exception in report.Event.Exceptions)
  {
    exception.Remove("message");
  }

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.

Request.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.

Request.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.

Request.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 } });
});

Request

If an error occurred in response to an HTTP request then the information about that request will be captured and available here.

Request.Bugsnag().BeforeNotify(report => {
  if (report.Event.Request != null)
  {
    report.Event.Request.Remove("headers");
  }
});

Severity

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

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

User

Set information about the user the error occurred for.

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