Writing Plugins For Windows Live Writer – Logging Errors

Sometimes your plugin will require some kind of logging, especially when it comes to logging errors that have occurred. So rather than reinvent the wheel for this, the Live Writer API has a class for doing just this, and logs it into the Windows Live Writer log file. So if you have implemented this in your plugin and you get an email come in with it giving errors or not working, you can request the log file and check what has been written. In the following example I’ll show you the code samples needed for logging to the WLW Log file.

This is a very simple thing to demonstrate, so I’ve created a simple plugin with a form and two buttons, one will call an error message box that looks like a normal Windows Live Writer error window as well as log it to the Windows Live Writer logfile. The second button will be used to try and open a file that doesn’t exist and so would normally throw an exception.

So the form looks like:

And when we click the first button we get:

When you then look in your logfile, you will see an entry at the end that says:

WindowsLiveWriter,1.11700,None,00017,05-Aug-2008 20:08:14.842,"DisplayableException occurred: WindowsLive.Writer.CoreServices.DisplayableException: Oh no, an error! – This is an error logged and displayed by the Plugin Diagnostics Plugin",""

Which as you can see is what is in that error window.

For the second button we get:

and again in the logfile:

WindowsLiveWriter,1.11200,Fail,00016,05-Aug-2008 22:55:49.828,"Unexpected Error Occurred
Exception Details:
An unexpected error has occurred within the application.
System.IO.FileNotFoundException: Could not find file ‘c:\scottisafool.scott’.
File name: ‘c:\scottisafool.scott’
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.Open(String path, FileMode mode)
   at PluginDiagosticsExample.frmMain.button2_Click(Object sender, EventArgs e)",""

With the second button, you’ll see that we aren’t presented with the standard Windows Live Writer error window, but a standard Windows error, the reason for this is because it’s the only way for the Writer team to get information about the error. What they then do is contact the plugin author and give them the details.

Now, to add this functionality to your plugin is dead simple:

        private void button1_Click(object sender, EventArgs e)
            // Log a general Error Message it to the user
            string message = "This is an error logged and displayed by the Plugin Diagnostics Plugin";

            // This one will show the error to the user as well as log it
            PluginDiagnostics.DisplayError("Oh no, an error!", message);

        private void button2_Click(object sender, EventArgs e)
            // Log an exception by opening a non-existent file
                File.Open(@"c:\scottisafool.scott", FileMode.Open);
            catch (Exception ex)
                PluginDiagnostics.LogException(ex, "From Plugin Diagostics Plugin");
                // This will display an error in the same way that Windows displays its own errors.

It’s as easy as that! With the first button, it’s up to you whether you display the error message or not (you might not need to). If you use the DisplayError() method, this gets written to the log file automatically, so although I show both methods in that example, it’s not necessary. This goes for the LogException() and DisplayUnexpectedException() methods too.

As always, you can download the source for this little sample from my skydrive area: PluginDiagosticsExample.zip

Windows Live Tags: , , , ,


Whilst writing this, I was listening to Linkin ParkOne Step Closer

  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: