Backtrace's integration with C# applications allows customers to capture and report handled and unhandled C# exceptions to their Backtrace instance, instantly offering the ability to prioritize and debug software errors.
// replace with your universe name and token
var backtraceCredentials =
new BacktraceCredentials(@"https://submit.backtrace.io/universe-name/token/json");
var backtraceClient = new BacktraceClient(backtraceCredentials);
try{
//throw exception here
}
catch(Exception exception){
await backtraceClient.SendAsync(new BacktraceReport(exception));
}
- Features Summary
- Supported .NET Frameworks
- Installation
- Running sample application
- Documentation
- Architecture
- Good to know
- Release Notes
- Light-weight C# client library that quickly submits C#/.NET exceptions and crashes to your Backtrace dashboard
- Can include callstack, system metadata, custom metadata, and file attachments (e.g. minidump) if needed.
- Supports a wide range of .NET versions such as .NET Framework, .NET Core, Mono, Xamarin and Unity. Read more here
- Supports both CLI and IDE work environments
- Supports asynchronous Tasks in .NET 4.5+
- Supports offline database for error report storage and re-submission in case of network outage
- Fully customizable and extendable event handlers and base classes for custom implementations
- Available as a NuGet Package as well as a fully open-sourced Github Release.
- .NET Framework 3.5 +
- .NET Framework 4.5 +
- getting information about application thread
- handling unhandled application exceptions
- .NET Standard:
- .NET Core 2.0 application
- Xamarin
- Universal Windows Platform
- Unity
- On Windows, we recommend Visual Studio 2017 or above for IDE. You can download and install Visual Studio here. As an alternative to
Visual Studio
you can use .NET Core command line interface, see installation guide here - On Mac OS X, you can download and install Visual Studio here if you prefer using an IDE. For command line, you should to download and install .NET Core 2.0 or above.
- On Linux, Visual Studio Code is available as a light-weight IDE. Similarly, you can use .NET Core command line interface, see instruction for Linux here
The Backtrace library is available on NuGet. You can read more about NuGet and how to download the packages here
You can install Backtrace via NuGet using the following commands:
Windows NuGet CLI:
Install-Package Backtrace
Linux/Mac OS X .NET Core CLI:
dotnet add package Backtrace
First create a BacktraceCredential
instance with your Backtrace endpoint URL
(e.g. https://xxx.sp.backtrace.io:6098) and submission token
, and supply it as a parameter in the BacktraceClient
constructor:
var credentials = new BacktraceCredentials("backtrace_endpoint_url", "token");
var backtraceClient = new BacktraceClient(credentials);
For more advanced usage of BacktraceClient
, you can supply BacktraceClientConfiguration
as a parameter. See the following example:
var credentials = new BacktraceCredentials("backtrace_endpoint_url", "token");
var configuration = new BacktraceClientConfiguration(credentials){
ClientAttributes = new Dictionary<string, object>() {
{ "attribute_name", "attribute_value" } },
ReportPerMin = 3,
}
var backtraceClient = new BacktraceClient(configuration);
For more information on BacktraceClientConfiguration
parameters please see Architecture.
Notes:
- If parameter
reportPerMin
is equal to 0, there is no limit on the number of error reports per minute. When thereportPerMin
cap is reached,BacktraceClient.Send/BacktraceClient.SendAsync
method will return false, - If you develop application behind the proxy you can pass
WebProxy
object toBacktraceCredentials
object. We will try to useWebProxy
object when user pass it toBacktrace
. To setup proxy use propertyProxy
, BacktraceClient
allows you to unpackAggregateExceptions
and send only exceptions that are available inInnerException
property ofAggregateException
. By defaultBacktraceClient
will sendAggregateException
information to Backtrace server. To avoid sending these reports, please overrideUnpackAggregateException
and set value totrue
.
BacktraceClient
allows you to customize the initialization of BacktraceDatabase
for local storage of error reports by supplying a BacktraceDatabaseSettings
parameter, as follows:
var dbSettings = new BacktraceDatabaseSettings("databaseDirectory"){
MaxRecordCount = 100,
MaxDatabaseSize = 1000,
AutoSendMode = true,
RetryBehavior = Backtrace.Types.RetryBehavior.ByInterval
};
var database = new BacktraceDatabase(dbSettings);
var credentials = new BacktraceCredentials("backtrace_endpoint_url", "token");
var configuration = new BacktraceClientConfiguration(credentials);
var backtraceClient = new BacktraceClient(configuration, database);
Notes:
- If a valid
databaseDirectory
directory is supplied, the Backtrace library will generate and attach a minidump to each error report automatically. Otherwise,BacktraceDatabase
will be disabled, - You can set
backtraceClient.MiniDumpType
toMiniDumpType.None
if you don't want to generate minidump files.
Backtrace C# library allows you to aggregate the same reports. By using Backtrace deduplication mechanism you can aggregate the same reports and send only one message to Backtrace Api. As a developer you can choose deduplication options. Please use DeduplicationStrategy
enum to setup possible deduplication rules or copy example below to setup deduplication strategy:
var dbSettings = new BacktraceDatabaseSettings(path)
{
DeduplicationStrategy = DeduplicationStrategy.LibraryName | DeduplicationStrategy.Classifier | DeduplicationStrategy.Message,
}
Deduplication strategy enum types:
- Ignore - ignore deduplication strategy,
- Default - deduplication strategy will only use current strack trace to find duplicated reports,
- Classifier - deduplication strategy will use stack trace and exception type to find duplicated reports,
- Message - deduplication strategy will use stack trace and exception message to find duplicated reports,
- LibraryName - deduplication strategy will use stack trace and faulting library name to find duplicated reports.
To combine all possible deduplication strategies please use code below:
DeduplicationStrategy = DeduplicationStrategy.LibraryName | DeduplicationStrategy.Classifier | DeduplicationStrategy.Message
Notes:
- When you aggregate reports via Backtrace C# library,
BacktraceDatabase
will store number of the same reports incounter
file. - Deduplication algorithm will include
BacktraceReport
Fingerprint
andFactor
properties.Fingerprint
property will overwrite deduplication algorithm result.Factor
property will change hash generated by deduplication algorithm. - By storing data in additional counter file we can read number of the same offline reports on application starts and send them to Backtrace when your internet connection back.
- When C# library aggregate multiple reports into one diagnostic data, application will send only one request, not multiple,
BacktraceDatabase
methods allows you to use aggregated diagnostic data together. You can checkHash
property ofBacktraceDatabaseRecord
to check generated hash for diagnostic data andCounter
to check how much the same records we detect.BacktraceDatabase
Count
method will return number of all records stored in database (included deduplicated records),BacktarceDatabase
Delete
method will remove record (with multiple deduplicated records) at the same time.- You can override default hash method by using
GenerateHash
delegate available inBacktraceDatabase
object. When you add your own method implementation,BacktraceDatabase
won't use default deduplication mechanism.
BacktraceClient.Send/BacktraceClient.SendAsync
method will send an error report to the Backtrace endpoint specified. There Send
method is overloaded, see examples below:
The BacktraceReport
class represents a single error report. (Optional) You can also submit custom attributes using the attributes
parameter, or attach files by supplying an array of file paths in the attachmentPaths
parameter.
try
{
//throw exception here
}
catch (Exception exception)
{
var report = new BacktraceReport(
exception: exception,
attributes: new Dictionary<string, object>() { { "key", "value" } },
attachmentPaths: new List<string>() { @"file_path_1", @"file_path_2" }
);
var result = backtraceClient.Send(backtraceReport);
}
Notes:
- if you initialize
BacktraceClient
withBacktraceDatabase
and your application is offline or you pass invalid credentials toBacktraceClient
, reports will be stored in database directory path, - for .NET 4.5+, we recommend to use
SendAsync
method, - if you don't want to use reflection to determine valid stack frame method name, you can pass
false
toreflectionMethodName
. By default this value is equal totrue
, BacktraceReport
allows you to change default fingerprint generation algorithm. You can useFingerprint
property if you want to change fingerprint value. Keep in mind - fingerprint should be valid sha256 string,BacktraceReport
allows you to change grouping strategy in Backtrace server. If you want to change how algorithm group your reports in Backtrace server please overrideFactor
property,Fingerprint
will overwriteBacktraceReport
deduplication strategy! In this case two different reports will use the same hash in deduplication algorithm, that could cause data lost,Factor
can change a result from deduplication algorithm. Hash generated by deduplication model properties will includeFactor
value.
If you want to use Fingerprint
and Factor
property you have to override default property values. See example below to check how to use these properties:
try
{
//throw exception here
}
catch (Exception exception)
{
var report = new BacktraceReport(...){
FingerPrint = "sha256 string",
Factor = exception.GetType().Name
};
....
}
For developers that use .NET 4.5+ and .NET Standard we recommend using SendAsync
method, which uses asynchronous Tasks. Both Send
and SendAsync
method returns BacktraceResult
. See example below:
try
{
//throw exception here
}
catch (Exception exception)
{
var report = new BacktraceReport(
exception: exception,
attributes: new Dictionary<string, object>() { { "key", "value" } },
attachmentPaths: new List<string>() { @"file_path_1", @"file_path_2" }
);
var result = await backtraceClient.SendAsync(backtraceReport);
}
BacktraceClient
can also automatically create BacktraceReport
given an exception or a custom message using the following overloads of the BacktraceClient.Send
method:
try
{
//throw exception here
}
catch (Exception exception)
{
//use extension method
var report = exception.ToBacktraceReport();
backtraceClient.Send(report);
//pass exception to Send method
backtraceClient.Send(exception);
//pass your custom message to Send method
await backtraceClient.SendAsync("Message");
}
BacktraceClient
allows you to attach your custom event handlers. For example, you can trigger actions before the Send
method:
//Add your own handler to client API
backtraceClient.BeforeSend =
(Model.BacktraceData model) =>
{
var data = model;
//do something with data for example:
data.Attributes.Add("eventAtrtibute", "EventAttributeValue");
if(data.Classifier == null || !data.Classifier.Any())
{
data.Attachments.Add("path to attachment");
}
return data;
};
BacktraceClient
currently supports the following events:
BeforeSend
AfterSend
RequestHandler
OnReportStart
OnClientReportLimitReached
OnUnhandledApplicationException
OnServerResponse
OnServerError
BacktraceClient
also supports reporting of unhandled application exceptions not captured by your try-catch blocks. To enable reporting of unhandled exceptions:
backtraceClient.HandleApplicationException();
Unhandled application exception handler will store your report in database. In case if you won't see your report in Backtrace, you will have to relaunch your application.
You can extend BacktraceBase
to create your own Backtrace client and error report implementation. You can refer to BacktraceClient
for implementation inspirations.
BacktraceReport
is a class that describe a single error report. Keep in mind that BacktraceClient
uses CallingAssembly
method to retrieve information about your application.
BacktraceClient
is a class that allows you to instantiate a client instance that interacts with BacktraceApi
. This class sets up connection to the Backtrace endpoint and manages error reporting behavior (for example, saving minidump files on your local hard drive and limiting the number of error reports per minute). BacktraceClient
extends BacktraceBase
class.
BacktraceClient
takes a BacktraceClientConfiguration
parameter, which has the following properties:
Credentials
- theBacktraceCredentials
object to use for connection to the Backtrace server.ClientAttributes
- custom attributes to be submitted to Backtrace alongside the error report.ReportPerMin
- A cap on the number of reports that can be sent per minute. IfReportPerMin
is equal to zero then there is no cap.
BacktraceData
is a serializable class that holds the data to create a diagnostic JSON to be sent to the Backtrace endpoint via BacktraceApi
. You can add additional pre-processors for BacktraceData
by attaching an event handler to the BacktraceClient.BeforeSend
event. BacktraceData
require BacktraceReport
and BacktraceClient
client attributes.
BacktraceApi
is a class that sends diagnostic JSON to the Backtrace endpoint. BacktraceApi
is instantiated when the BacktraceClient
constructor is called. You use the following event handlers in BacktraceApi
to customize how you want to handle JSON data:
RequestHandler
- attach an event handler to this event to override the defaultBacktraceApi.Send
method. ARequestHandler
handler requires 3 parameters -uri
,header
andformdata
bytes. DefaultSend
method won't execute when aRequestHandler
handler is attached.OnServerError
- attach an event handler to be invoked when the server returns with a400 bad request
,401 unauthorized
or other HTTP error codes.OnServerResponse
- attach an event handler to be invoked when the server returns with a valid response.
BacktraceApi
can send synchronous and asynchronous reports to the Backtrace endpoint. To enable asynchronous report (default is synchronous) you have to set AsynchronousRequest
property to true
.
BacktraceResult
is a class that holds response and result from a Send
or SendAsync
call. The class contains a Status
property that indicates whether the call was completed (OK
), the call returned with an error (ServerError
), the call was aborted because client reporting limit was reached (LimitReached
), or the call wasn't needed because developer use UnpackAggregateException
property with empty AggregateException
object (Empty
). Additionally, the class has a Message
property that contains details about the status. Note that the Send
call may produce an error report on an inner exception, in this case you can find an additional BacktraceResult
object in the InnerExceptionResult
property.
BacktraceDatabase
is a class that stores error report data in your local hard drive. If DatabaseSettings
dones't contain a valid DatabasePath
then BacktraceDatabase
won't generate minidump files and store error report data.
BacktraceDatabase
stores error reports that were not sent successfully due to network outage or server unavailability. BacktraceDatabase
periodically tries to resend reports
cached in the database. In BacktraceDatabaseSettings
you can set the maximum number of entries (MaxRecordCount
) to be stored in the database. The database will retry sending
stored reports every RetryInterval
seconds up to RetryLimit
times, both customizable in the BacktraceDatabaseSettings
.
BacktraceDatabaseSettings
has the following properties:
DatabasePath
- the local directory path whereBacktraceDatabase
stores error report data when reports fail to sendMaxRecordCount
- Maximum number of stored reports in Database. If value is equal to0
, then there is no limit.MaxDatabaseSize
- Maximum database size in MB. If value is equal to0
, there is no limit.AutoSendMode
- if the value istrue
,BacktraceDatabase
will automatically try to resend stored reports. Default isfalse
.RetryBehavior
- -RetryBehavior.ByInterval
- Default.BacktraceDatabase
will try to resend the reports every time interval specified byRetryInterval
. -RetryBehavior.NoRetry
- Will not attempt to resend reportsRetryInterval
- the time interval between retries, in seconds.RetryLimit
- the maximum number of timesBacktraceDatabase
will attempt to resend error report before removing it from the database.
If you want to clear your database or remove all reports after send method you can use Clear
, Flush
and FlushAsync
methods.
ReportWatcher
is a class that validate send requests to the Backtrace endpoint. If reportPerMin
is set in the BacktraceClient
constructor call, ReportWatcher
will drop error reports that go over the limit. BacktraceClient
check rate limit before BacktraceApi
generate diagnostic json.
You can use this Backtrace library with Xamarin if you change your HttpClient
Implementation to Android
. To change HttpClient
settings, navigate to Android Options
under Project Settings
and click on Advanced
button.
See release notes here.