How to sleep in (as a developer)

Each morning at around 7:30am, someone uses an application I wrote to load a trading system with data so that traders can make their clients money. If the app fails, then they can’t really do this. As the app was going live, I feared I might have to set my alarm every day that week. If it’s not already apparent, I am a night owl through and through.

Agatha Christie famously said, “invention, in my opinion, arises directly from idleness, possibly also from laziness.” This was very true in my quest to not have to set an alarm. I had already added robust error logging that would send me an email if the app crashed. Here’s the code for that, by the way. It’s pretty Fisher Price My First Global class, but it gets the job done.

public static void LogError(string error, Exception exception)
    //Write to error log, in case email notification fails
    string dtNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    string strFilePath = 
      new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly()
      + @"\" + "myapp_error_log.txt").LocalPath;
    StreamWriter sw;

    //IF file doesn't exist
    if (!File.Exists(strFilePath))
        sw = new StreamWriter(File.Create(strFilePath));
        sw = new StreamWriter(strFilePath, true);

    sw.WriteLine("**** ERROR ****");
    sw.WriteLine("Time: " + dtNow);
    sw.WriteLine("Error: " + error);
    if (exception != null)
        sw.WriteLine("Exception: " + exception.Message);
        sw.WriteLine("Stack Trace: " + exception.StackTrace);

    //Send email with error to support
    SendErrorEmail(error, exception);

public static void SendErrorEmail(string error, Exception exception)
    MailMessage mail = new MailMessage();
    string dtNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    mail.From = new MailAddress(Environment.UserName + "");
    if (ConfigurationManager.AppSettings.Count > 1)
        var to = ConfigurationManager.AppSettings["ErrorEmailTo"];

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Error in myapp");
    sb.AppendLine("Time: " + dtNow);
    sb.AppendLine("Machine: " + Environment.MachineName);
    sb.AppendLine("User: " + Environment.UserName);
    sb.AppendLine("Error: " + error);
    if (exception != null)
        sb.AppendLine("Exception: " + exception.Message);
        sb.AppendLine("Stack Trace: " + exception.StackTrace);

    SmtpClient client = new SmtpClient("");
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    mail.Subject = "Error in myapp";
    mail.Body = sb.ToString();

I just needed a way for my phone to wake me up if the application generated an error email.

I first experimented with the Gmail app for iPhone. In Notifications, you can make the app play a sound when a new email is received. The problem is the sound is just a quick beep–nothing that would wake me up from deep REM.

I then tried the built-in Mail application in iOS. It looked more promising because you can configure the sound to be a legitimate phone ring. I added my Gmail account, but after running a quick test, I realized that there was a big delay between when the email was sent and when it arrived in the Mail app. In Mail settings, iOS gives the ambiguous explanation that “new data will be pushed to your iPhone from the server when possible,” and it turns out that the fastest fetch schedule you can set is every 15 minutes. I guess Google doesn’t push rapidly because they want people to use their Gmail app. Worst.

After some searching, I figured out that you can send a text to your phone using a standard email address. I think I did this way back in the mid 2000s, but totally forgot about it. Here’s an exhaustive list of the email formats for different carriers. This enabled me to receive a text on my phone by having Gmail forward the application error email to [myphone#] Here are the steps to set up.

  1. Implement error handling and emails using something like the code provided above.
  2. Send the error emails to Gmail. You could send it directly to your phone, but it’s good to have error information in a more readable format than SMS.
  3. In Gmail, go to Settings > Forwarding and POP/IMAP, and click add a forwarding address. Use your phone number in your carrier’s format (see the link above).
  4. Gmail will keep forwarding disabled after it’s added, which is good. Now go to Settings > Filters and Blocked Addresses, and create a filter for your error email. Using the code above, it would be Subject = Error in myapp.
  5. On the following page, check Forward it to, and select your cell phone’s email address, which should be available in the dropdown. This will enable forwarding for this specific filter.
  6. On your phone, choose a text sound that will wake you up.
  7. Stay up late and forget the alarm.

You can get creative with these alerts. For example, you can set up monitoring using a free service like Uptime Robot and get texts if a site goes down, or you can setup a DIY security system using motion and a Raspberry Pi (see my previous post for more info on home automation).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s