This is a screenshot of a log viewed in Gmail.
This is a follow up to the post So I rolled my own logger. In that post I provided a simple logger class.
In this post I use http to send all the logs rolled up into one text file to a php script which emails the file to me. Remember, iOS does not allow you to send email programmatically.
// helper function to retirn path
public static string GetLocalFilesPath()
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return Path.Combine(documents, "..", "Library", "Caches");
public static void EmailLogs()
const string url = "http://example.com/email.php";
var sb= new StringBuilder();
var dirInfo = new DirectoryInfo(Support.GetLocalFilesPath());
var lstFiles = dirInfo.GetFiles("*.log");
foreach (var info in lstFiles)
var fInfo = info; // avoids closure bug
using (var sr = new StreamReader(fInfo.FullName))
var msg = WebUtility.UrlEncode(sb.ToString());
var fullMsg = "z=xyz23&mid="+UNIT.MobileID+@"&alphasid="+UNIT.SystemID+@"&lui="+msg;
Task.Factory.StartNew(() => HttpPost(url, fullMsg), TaskCreationOptions.LongRunning);
foreach (var info in lstFiles)
Support is a class that includes a static instance of the logger class. This is a wrapper round a StreamWriter so the pause method closes the StreamWriter otherwise we’d be tring to read from a file that[‘s open in the StreamWriter. After the logs have been sent off the logger is restarted.
This rolls up all the logs it finds into one file using StringBuilder to append the file name and then each line in that log then url encodes it and calls the function httppost below which I found on Microsoft’s site
Note that for security I add in a few parameters. (MobileId and SystemID are static properties from another unit; I’ve changed that to UNIT as my unit name would be meaningless.) Once again I use fire and forget for calling httppost. Ie run it in another Task using the TPL. I’m deliberately not checking any error messages.
public static string HttpPost(string url, string parameters)
//Create a WebRequest
var req = (HttpWebRequest)WebRequest.Create(url);
//Set the content type and method
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//Get the total size of the post parameters and set the content length
var bytes = System.Text.Encoding.UTF8.GetBytes(parameters);
req.ContentLength = bytes.Length;
//Write the data to the request stream
Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
//Get the response
WebResponse resp = req.GetResponse();
if (resp == null) return null;
//Get the response stream and read the response
var sr = new StreamReader(resp.GetResponseStream());
string result = sr.ReadToEnd().Trim();
//Close the streams
Support.log = new Logger();
Finally I have a small PHP script setup on a domain hosted on a shared server. Note the use of security parameters to stop spammers using this script to bombard you with spam. The parameters below should match the parameters in this line from above:
var fullMsg = "zvf=xyz23&mid="+UNIT.MobileID+@"&alphasid="+UNIT.SystemID+@"&lui="+msg;
This is the PHP script below, after validating that it’s a genuine request, it takes the three parameters, appends the log file to a local text file then emails it to the specified email address.
I have the error messages commented out as they’re only there for debugging purposes.