A .NET Rollbar Client for Xamarin Forms, based on the project code Rollbar.NET
In the Android and iOS projects, install the following package:
- Json.NET 10.0.3
In all PCL projects where the Rollbar plugin is added, installed the following nuget packages :
- Json.NET 10.0.3
The RollbarConfig
object allows you to configure the Rollbar library.
- AccessToken
- The access token for your project, allows you access to the Rollbar API
- Endpoint
- The Rollbar API endpoint, defaults to https://api.rollbar.com/api/1/
- Environment
- Environment name, e.g. `"production"` or `"development"` defaults to `"production"`
- Enabled
- If set to false, errors will not be sent to Rollbar, defaults to `true`
- LogLevel
- The default level of the messages sent to Rollbar
- Transform
- Allows you to specify a transformation function to modify the payload before it is sent to Rollbar
new RollbarConfig { Transform = payload => { payload.Data.Person = new Person { Id = 123, Username = "rollbar", Email = "[email protected]" }; } }
You can set the current person data with a call to
Rollbar.Current.PersonData(() => new Person
{
Id = 123,
Username = "rollbar",
Email = "[email protected]"
});
and this person will be attached to all future Rollbar calls.
You can set the current model with a call to
RollbarDotNet.Data.SetModel("model device");
You can use the nuget plugin Xam.Plugin.DeviceInfo to easily get the model.
You can set the current application version with a call to
RollbarDotNet.Data.SetAppVersion("application version");
You can use the nuget plugin Plugin.VersionTracking to easily get the application version.
You can set the current OS version with a call to
RollbarDotNet.Data.SetOSVersion("android version");
You can use the nuget plugin Xam.Plugin.DeviceInfo to easily get the OS version.
If you want more control over sending the data to Rollbar, you can fire up a RollbarClient
yourself, and make the calls directly. To get started you've got exactly one interesting class
to worry about: RollbarPayload
. The class and the classes that compose the class cannot be
constructed without all mandatory arguments, and mandatory fields cannot be set.
Therefore, if you can construct a payload then it is valid for the purposes of
sending to Rollbar. To get the JSON to send to Rollbar just call
RollbarPayload.ToJson
and stick it in the request body.
There are two other particularly interesting classes to worry about:
RollbarData
and RollbarBody
. RollbarData
can be filled out as completely
or incompletely as you want, except for the Environment
("debug",
"production", "test", etc) and and RollbarBody
fields. The RollbarBody
is
where "what you're actually posting to Rollbar" lives. All the other fields on
RollbarData
answer contextual questions about the bug like "who saw this
error" (RollbarPerson
), "What HTTP request data can you give me about the
error (if it happened during an HTTP Request, of course)" (RollbarRequest
),
"How severe was the error?" (Level
). Anything you see on the
rollbar api website can be found in
Rollbar.NET
.
RollbarBody
can be constructed one of 5 ways:
- With a class extending from
Exception
, which will automatically produce aRollbarTrace
object, assigning it to theTrace
field of theRollbarBody
. - With a class extending from
AggregateException
, which will automatically produce an array ofRollbarTrace
objects for each inner exception, assigning it to theTraceChain
field ofRollbarBody
. - With an actual array of
Exception
objects, which will automatically produce an array ofRollbarTrace
objects for each exception, assigning it to theTraceChain
field ofRollbarBody
. - With a
RollbarMessage
object, which consists of a string and any additional keys you wish to send along. It will be assigned to theMessage
field ofRollbarBody
. - With a string, which should be formatted like an iOS crash report. This
library has no way to verify if you've done this correctly, but if you pass in
a string it will be wrapped in a dictionary and assigned to the
"raw"
key and assigned to theCrashReport
field ofRollbarBody
None of the fields on RollbarBody
are updatable, and all null fields in
Rollbar.NET
are left off of the final JSON payload.
Initialize the rollbar plugin in the MainActivity.cs
class
[Init other plugins]
...
RollbarDotNet.Droid.Rollbar.Init(new RollbarConfig("SERVER_TOKEN"));
...
LoadApplication(new App());
Initialize the rollbar plugin in the AppDelegate.cs
class
[Init other plugins]
...
RollbarDotNet.iOS.Rollbar.Init(new RollbarConfig("SERVER_TOKEN"));
...
LoadApplication(new App());
await Rollbar.Current.Report(ex, ErrorLevel.Error);