home | articles | site map | contacts
about us
client login
Remote Debugging with Pastebin, Automatic Posting to Pastebin in C# .NET
9/12/2011 (Modified 9/13/2011)
Follow PrimaryObjects on Twitter Subscribe to Primary Objects via RSS More Software Articles
by Kory Becker
enter email address


Debugging software applications typically occurs within the context of an IDE (integrated development environment), such as Eclipse, XCode, Visual Studio, etc. This allows developers to step line by line through sections of code, analyzing variables, and tracing the stack. Occasionally, developers may be unable to debug within the IDE, whether due to software complexity, runtime environments, or other issues. During these conditions, resourceful developers may utilize more primitive, but equally powerful methods of debugging, such as raw printing of values and steps (printf, cout, alert statements, etc). However, when the software process executes in a remote server environment, where the developer may not have access to the local file system, debugging can take on additional complexity. Common remote debugging solutions include writing logs to the file system, email, or remote web service methods.

In this tutorial, we'll discuss using the Pastebin web service to assist with remote debugging. We'll include a simple code sample for accessing the Pastebin API and posting output.

 What is Pastebin?

Pastebin is an online service for posting and uploading any type of text. After posting, pastebin provides a short link for accessing the post or sharing with others. Pastebin, along with a list of other simple text posting services, are usually easy to use and can be particularly useful for cases of simplified remote debugging.

All Code Should Work Locally, Of Course

Software environments vary by organization and stage, including development, QA, and production. In a perfect world, software functions the same, whether installed on different machines or staging environments. However, as subtle differences may exist within each environment, developers may find that code operates differently when deployed to different locations. This may be as a result of security differences, linked libraries, installed software, or other conditions. However, this usually results in the need for remote debugging.

Limitations of Remote Debugging

Depending on the IDE, remote debugging usually requires debugging software to be installed on the remote server (such as, Visual Studio's remote debugging service), which allows the IDE to link to the remote server while executing code. In some cases, the developer may have limited access to the remote machine, specifically with regard to installing software.

On to the File System

In cases where software installation access is limited on the remote machine, the next method of debugging is typically to print to a local log file, registry, or email. Variables, lines, output, and other values may be logged to a file or sent by email for retrieval by the developer. This method works exceptionally well as a fallback for many types of debugging. However, in certain cases of debugging where software complexity or integrated software is involved, local logging may be unavailable.

To the Web and Beyond

When local file logging fails, an alternative solution can be web service logging. Web service logging can often take the same debugging content as a a file, email, or registry log. This allows the developer to easily monitor a web-based log to track the performance of the application. The easier the web service is to integrate with, the easier the task of remote debugging. One (of many) easy web services for posting simple text, includes Pastebin.

The Pastebin Client

Pastebin provides an easy URL-based API for automatically posting text from a variety of applications. We can take advantage of the Pastebin API to post our software application's debug output by creating a simple Pastebin client in C# .NET to automatically upload text to pastebin, as follows:
public static class PasteBin
        private const string _pasteBinUrl = "http://pastebin.com/api/api_post.php";
        private static string _devKey = ConfigurationManager.
AppSettings["pasteBinDeveloperApiKey"]; /// <summary> /// Creates a new PasteBin post, with a default name. /// </summary> /// <param name="text">Text to post</param> /// <returns>PasteBin URL</returns> public static string Create(string text) { return Create("My Paste", text); } /// <summary> /// Creates a new PasteBin post. /// </summary> /// <param name="name">Name of post</param> /// <param name="text">Text to post</param> /// <returns>PasteBin URL</returns> public static string Create(string name, string text) { string pasteBinUrl = ""; // Create the request. WebRequest wr = WebRequest.Create(_pasteBinUrl); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] bData = encoding.GetBytes("api_option=paste&api_dev_key=" +
_devKey + "&api_paste_code=" +
HtmlEncoder.UrlEncode(text) +
"&api_paste_private=1&api_paste_expire_date=1D&api_paste_name=" +
HtmlEncoder.UrlEncode(name)); wr.Method = "POST"; wr.ContentType = "application/x-www-form-urlencoded"; wr.ContentLength = bData.Length; // Send the request. using (Stream sMyStream = wr.GetRequestStream()) { sMyStream.Write(bData, 0, bData.Length); } // Read the response. using (WebResponse response = wr.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { pasteBinUrl = reader.ReadToEnd(); } } return pasteBinUrl; } }
The above code can be similarly used with almost any text storage web service. The code simply wraps a web request to the pastebin API service, and passes in the developer API key. The codes sets the option for "Paste", indicating that we are creating a new Pastebin document, provides the text to post, and sets the post to private.

Our software application under test can now utilize the Pastebin client to post debug output logs.

Using the Pastebin Client

Using the pastebin client code is as simple as the following example:
 static void Main(string[] args)
    string url = PasteBin.Create("Hello World, from PasteBinSample!");

Of course, in your actual application being debugged, you could include the PasteBin.Create() call in each place where you would typically include a printf() statement to output a variable or step. Utilizing a text-posting web service in this manner provides another tool in the developer's arsenal, helping to increase software development productivity.

Finally, a Word on Responsibility

In any case where 3rd-party web services are involved, it's important to keep in mind security and privacy considerations. This is especially the case when posting, potentially sensitive, software debug output. In cases where security and privacy of the outputted debug values are of importance, developers would be better served by creating their own local logging web service, in which case they can post debug info to a locally provided url. However, in cases where software application debug information is non-sensitive, web services can be an excellent tool for remote debugging productivity.

Download @ GitHub

The source code for this example is available online in our GitHub repository.
About the Author

This article was written by , Microsoft certified software developer and architect, providing C# ASP .NET web application development, database design, and mobile software development across a variety of domains for clients in both the business and consumer sectors.


comments powered by Disqus
Learn more about Primary Objects and our goals ..  More
Primary Objects publishes node.js app, RedAnt, a REST web service .. More
Primary Objects publishes node.js app, CD Early Withdrawal Calculator .. More
Home | About Us | Services | Client Login | Job Opportunities | Contact Us
Copyright © Primary Objects 2013
Privacy Policy
Follow us on Twitter