Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created February 2, 2016 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maritaria/a412620e26fd03bab32c to your computer and use it in GitHub Desktop.
Save maritaria/a412620e26fd03bab32c to your computer and use it in GitHub Desktop.
Simple error reporting example code
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Serialization;
namespace HotBot.Core
{
public static class ErrorReporting
{
//Call ErrorReporting.Init() as soon as possible when the game starts
//THIS CODE IS NOT OF ANY QUALITY BUT OUTLINES A BASIC SYSTEM
//WILL (probably) NOT WORK OUT OF THE BOX
//BUT SHOULD REQUIRE MINIMAL EFFORT TO GET WORKING
//All logging is send to a server at some url(below)
//And is formatted in XML on the POST parameter "data"
private static string LandingSite = "http://crashreport.skyhook.com/newlog.php";//Where should reports be send to?
private static bool _initialized = false;
public static void Init()
{
//TODO: Not thread safe
if (!_initialized)
{
_initialized = true;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (!(e.ExceptionObject is Exception))
{
return;
}
else
{
PostException((Exception)e.ExceptionObject);
}
}
private static void PostException(Exception exceptionObject)
{
//Setup request
var request = (HttpWebRequest)WebRequest.Create(LandingSite);
//Put the data to transmit into a string
var serializedException = "data=" + exceptionObject.SerializeException();
var encodedData = Encoding.ASCII.GetBytes(serializedException);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = encodedData.Length;
//Write string to the stream
using (var stream = request.GetRequestStream())
{
stream.Write(encodedData, 0, encodedData.Length);
}
//Wait for response and read it to close the connection
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//TODO: Handle networking issues
//TODO: Notify player of the request
//TODO: Make sure this shit is covered in the TSA or the user is asked whether they want the report to be sent (report contains no personal data)
}
private static string SerializeException(this Exception exception)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializableException));
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, new SerializableException(exception));
return textWriter.ToString();
}
}
[Serializable]
private class SerializableException
{
public DateTime TimeStamp { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public SerializableException()
{
TimeStamp = DateTime.Now;
}
public SerializableException(string message) : this()
{
Message = message;
}
public SerializableException(System.Exception ex) : this(ex.Message)
{
StackTrace = ex.StackTrace;
}
public override string ToString()
{
return Message + StackTrace;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment