Using the breadcrumbs feature for Raygun Crash Reporting, you can attach an accurate trail of the events through your system leading up to the moment a crash report was generated. You’ll gain more insight into why the application crashed when it did, making prioritization of fixes much easier.
Check out the Breadcrumbs feature announcement for more on this feature.
Raygun4Net is one of the providers that supports breadcrumb recording. Unlike the Raygun4JS provider that automatically records events such as UI clicks and XHR calls, you need to manually record events in your server-side code.
Even though breadcrumbs in Raygun4Net doesn’t come with automatic event logging, if you’re like us at Raygun, you are also using a logging framework such as log4net or NLog. If you are – you’re in luck!
Capturing log messages from log4net
If you’re using log4net in your application then capturing your current log messages in a breadcrumb is as simple as adding a custom appender. The appender will extract all the required information from the logging event and append it to the breadcrumb trail. If an exception occurs at any point in your code, then the trail of log messages leading up the error will automatically be sent as a breadcrumb along with the error report to Raygun.
As per standard log4net configuration, the breadcrumb message is defined in your appender config through a layout configuration section. This allows you to control the breadcrumb messages through configuration.
In the following example, I have configured all debug log messages to be recorded. In the conversionPattern I have only added the message to be output. Other data (such as the class name, method name, line number, thread, and timestamp) is all automatically added to the breadcrumb by the appender.
<appender name="myAppAppender" type="Raygun.Examples.RaygunBreadcrumbAppender">
<conversionPattern value="%message" />
<level value="DEBUG" />
<appender-ref ref="myAppAppender" />
Start capturing log4net log messages now by checking out the full code and configuration examples in this gist: https://gist.github.com/j5alive/87ed7d0b88ce81d65475e512606aacb2
Capturing log message from NLog
If you’re using NLog in your application then capturing your current log messages in a breadcrumb is as simple as adding a custom target. The target will extract all the required information from the log event info and append it to the breadcrumb trail. If an exception occurs at any point in your code, then the trail of log messages leading up the error will automatically be sent as a breadcrumb along with the error report to Raygun.
The NLog log event does not automatically capture the stack trace at the time you log a message. However, we’ve added a configuration property to the NLog target that will allow the custom target to create the stack trace and automatically determine the point that your code called the log method. This, in turn, allows the target to record the class name, method name and line number in the breadcrumb.
In the following example, I am configuring all debug log messages to be recorded in my breadcrumbs along with class and method names by setting the includeMethodInfo property to true .
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<target name="breadcrumb" xsi:type="RaygunBreadcrumb" includeMethodInfo="true" />
<logger name="*" minlevel="Debug" writeTo="breadcrumb" />
Start capturing NLog log messages now by checking out the full code and configuration examples in this gist: https://gist.github.com/j5alive/2d24b40fa3e0b101b5f6d91b4af4aa5d
Capturing controller actions from your MVC application
If you’re not using a logging framework but you do have an ASP.NET MVC web application, then you can start capturing controller actions that are invoked along with the parameters and values to a breadcrumb. This is done by adding a custom ActionFilterAttribute to your MVC application and then register the attribute at a controller, method or even at a global level.
The breadcrumb attribute will capture all action parameters along with the controller class name and action method name. The parameter names and values automatically get added as custom data to the breadcrumb. The great thing about this is that if an exception occurs in the execution of an action, you can look into the breadcrumb to see values that were passed into the action, making it easier for you to reproduce the exact state of the system when the error occurred!
In my example below, I register the attribute as a global filter, meaning it is attached to all actions in my MVC application. You’ve probably got a similar method in your code already. Simply add the RaygunBreadcrumbAttribute class to your solution and register it like so:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
Start capturing ASP.NET controller actions along with their parameters and values now by checking out the full code example in this gist: https://gist.github.com/j5alive/03d16dc5c950a873b7073f77c2eb3b36
Note: we plan to add the RaygunBreadcrumbAttribute class to the Raygun4NET.Mvc library in the near future to make this even easier to add to your web application.
Breadcrumb logging in Raygun4Net
This screenshot shows:
1. The MVC Attribute automatically logging the action along with the parameter names and values (CategoryId and ProductId)
2. Automatic Info log message from log4net in the controller method
3. Another automatic Debug log message from log4net from another method
Other logging frameworks
These examples will make it easy to start logging more useful information to a breadcrumb alongside your current error reports. If you are using a different logging framework and would like help implementing automatic breadcrumb logging in Raygun4Net then please get in touch with the team.