Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Error Reporting with PushingBox

0.00/5 (No votes)
30 May 2013CPOL3 min read 22.2K  
I’ll explain a little about this free service and show you some sample code of how to integrate it into your applications or automation scripts using Powershell.

One of my users let me know they were using a cloud service called PushingBox to be notified of events in Directory Monitor. I investigated what it can do and immediately thought of a couple of interesting use cases for this service. One of those use cases being the ability to send errors to yourself from your own software in the wild. I’ll explain a little about this free service and show you some sample code of how to integrate it into your applications or automation scripts using Powershell.

The PushingBox Service

PushingBox is a free service to anyone with a Google account. You can login and it will generate a demo email scenario for you automatically. There are however many more services you can link up to including popular iPhone and Android notification apps and even Twitter. The API is simply an HTTP GET or POST using the “DeviceID” assigned to your scenario and optional parameters that you can use to augment your push messages.

Setting up a Basic Scenario

To get an email for errors in your application, setup a simple email scenario in PushingBox (My Scenarios –> Create scenario). You can use parameter names enclosed in $ signs for replacement when you make a call to the service (remember this isn’t limited to just email):

PushingBox scenrio

Now you can use parameters with the same name in the URL or in your POST and they will be replaced with those values when the email arrives in your inbox. When you’ve setup the scenario, remember to grab the “DeviceID”, you’ll need that to make a request to the service.

Making the Service Request in C#

The code to make a HTTP POST in C# (or .NET in general) is very easy. Here is a basic static method (using statements left out for brevity) that will POST an exception to the service, remember to use your own devid in place of “YOUR_DEV_ID_HERE”.

C#
public static class PushingBoxHelper
{
  public static void PushError(string version, Exception error)
  {
    Contract.Requires(!string.IsNullOrEmpty(version));
    Contract.Assume(error != null);

    // Fire and forget
    Task.Factory.StartNew(() =>
    {
      try
      {
        var post = new NameValueCollection();
        post.Add("devid", "YOUR_DEV_ID_HERE");
        post.Add("version", version);
        post.Add("error", error.ToString());

        using (var wc = new WebClient())
        {
          wc.UploadValues("http://api.pushingbox.com/pushingbox", post);
        }
      }
      catch (WebException we)
      {
        Log.Error("Failed to submit error information to PushingBox.", we);
      }
    });
  }
}

Now, whenever you get an error or unhandled exception in your application that you want to get notified about, just call the PushingBoxHelper.PushError method and it will quietly try to push the information to PushingBox’s API so you will get an email or a push to any other service you have configured.

Making the Service Request in Powershell

If you do not want to write any code or just need to push a notification from a script, you can easily use any of the examples on the API section of their site. I opted for a Powershell script since I wouldn’t need any other dependencies such as Python, cURL or PHP.

Be careful of doing a GET since you will need to encode the parameters correctly otherwise data may get lost. This is the Powershell code to make a GET request:

PowerShell
##############
# GET Method #
##############
param (
  [string]$version = "",
  [string]$err = ""
)

begin {
  [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
  $get = "http://api.pushingbox.com/pushingbox?devid=YOUR_DEV_ID_HERE&version="
    + [System.Web.HttpUtility]::UrlEncode($version) + "&error=" 
    + [System.Web.HttpUtility]::UrlEncode($err)
  (new-object System.Net.WebClient).DownloadString($get)
}

In the same light, you could also POST from Powershell in a very similar way. I prefer this because it’s cleaner and you don’t need to encode anything, it will happen automatically for you:

PowerShell
###############
# POST Method #
###############
param (
  [string]$version = "",
  [string]$err = ""
)

begin {
  [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
  $post = new-object System.Collections.Specialized.NameValueCollection
  $post.Add("devid", "YOUR_DEV_ID_HERE")
  $post.Add("version", $version)
  $post.Add("error", $err)
  $wc = new-object System.Net.WebClient
  $wc.UploadValues("http://api.pushingbox.com/pushingbox", $post)
}

You can execute this script from the command-line using the named parameters to send the information you need:

PowerShell
powershell -file pushingbox.ps1 -version "1.2.3.4" -err "Something went wrong!"

Conclusion

I think this is an awesome service that could be used for a variety of scenarios where you would want instant notification via multiple channels when something interesting happens. Breaking builds triggering a Karotz, automating Tweets when you create a blog entry, there are so many… They also provide the source for certain Arduino shields so you can push notifications due to electrical/mechanical interaction with the environment. Check out the videos on the home page for more cool ideas.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)