Logging JSon data to Rollbar
Using Rollbar is great for Exceptions both handled and unhandled. But there is so much more you can get out of a solution like Rollbar.
We often hear users asking us to log structured log data to Rollbar or that they have a custom JSon object that they would like to store in Rollbar.
If you just send in the Json object using Rollbar.Info(YOUROBJECT) you will find that it is placed in the message body as a string. This is fine but we can do a lot better.
In my example below I want to use Rollbar to accept my logs from a .NET 7 API. I log everything to Rollbar not just exceptions. So I have created my own object that contains a set of properties and I would like to send that into Rollbar as something more than just a string message.
Using Rollbar Payload Transformer
Lets look atthe code example to use the Payload Transform function to modify the payload before it is sent to Rollbar.
//Global Variables for access token and Environment
const string rollbarAccessToken = "xxxxxxxxxxxxxxxxxxxxxx"; //Add your Server Side (Post) Rollbar Access Token
string rollbarEnvironment = "production";
//Lets auto identify the Environment
#if DEBUG
// debug stuff goes here
rollbarEnvironment = "development";
#else
// release stuff goes here
rollbarEnvironment = "production";
#endif
//Send to Error Monitoring Solution
RollbarLoggerConfig rollbarConfig = new RollbarLoggerConfig(rollbarAccessToken, rollbarEnvironment);
RollbarLocator.RollbarInstance.Configure(rollbarConfig);
//Lets send in a custom JSon Object butplace it into the Custom Object in Rollbars Payload.
var json = JsonConvert.SerializeObject(YOURCUSTOMJSONOBJECTHERE);
var dictionary = JsonConvert.DeserializeObject>(json);
RollbarPayloadManipulationOptions rollbarPayload = new RollbarPayloadManipulationOptions()
{
Transform = payload => {
payload.Data.Custom = dictionary;
payload.Data.Environment = rollbarEnvironment;
}
};
RollbarLocator.RollbarInstance.Configure(rollbarConfig);
//Lets send a Log
RollbarLocator.RollbarInstance.Info("Item Display Name", dictionary);
//Or maybe an Error
RollbarLocator.RollbarInstance.Error("Item Display Name", dictionary);
What does it look like in Rollbar?
Now we have each field in my JSon object as a custom payload value in Rollbar.
In this example I log my own logtype, subscriptionid, userid, appservice fields... but you can add anything you like to this.
We hope this helps you expand your usage of Rollbar for more use cases.
Track, Analyze and Manage Errors at Scale
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing errors easier than ever. Sign Up Today!