Friday, December 25, 2009

A Simple Twitter Logger

[UPDATE! Look here for the OAuth version.]



Yes, using The Object Guy's Logging Framework it is incredibly easy to create a logger that will write to Twitter.

Here's a sample:

public class TwitterLogger : Logger
{  
  protected override bool DoLog(LogEntry aLogEntry)
  {
    // without this, you may receive a '417 Expectation Failed' error
    ServicePointManager.Expect100Continue = false;

    using (WebClient webClient = new WebClient())
    {
       webClient.Credentials = new NetworkCredential("[YOUR USER NAME]", "[YOUR PASSWORD]");

       var nvc = new NameValueCollection();
       nvc["status"] = aLogEntry.Message;

       webClient.UploadValues("http://twitter.com/statuses/update.xml", nvc);
    }
    return true;
  }
}

Remember that Twitter may limit your status updates to 150 per hour and a thousand or so per day.

6 comments:

  1. Hey, that's neat.

    ReplyDelete
  2. I'm getting a 417 exception failed errorat WebClient.UpladValues(Uri address, String method, NameValueCollection data)

    Do you know any fixes?

    ReplyDelete
  3. Look carefully at the code I posted. You'll see that I address the 417 error.

    ReplyDelete
  4. Sorry about that, I totally missed it. Now I'm getting a 403 Forbidden error. Do you know what could be causing that?

    ReplyDelete
  5. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. smm panel

    ReplyDelete