Outlook Add-in: Monitor add-ins load time

An Extensibility Add-in code sample for monitoring Add-ins load time in Outlook: The add-in uses the OnStartupComplete event, gets the Outlook process start time and writes an event in the Application event log.

The event will be similar to:

Log Name:       Application
Source:            Outlook Startup Monitor
Date:               27/01/2012 14:22:54
Event ID:          1
Task Category: None
Level:               Information
Keywords:        Classic
Message:         Outlook was started at 03/02/2012 08:25:39 and it finished loading add-ins at 03/02/2012 08:30:25

namespace OutlookStartupMonitor{    using System;    using Extensibility;    using System.Runtime.InteropServices;    using System.Reflection;    using System.Diagnostics;

    #region Read me for Add-in installation and setup information.    // When run, the Add-in wizard prepared the registry for the Add-in.    // At a later time, if the Add-in becomes unavailable for reasons such as:    // 1) You moved this project to a computer other than which is was originally created on.    // 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.    // 3) Registry corruption.    // you will need to re-register the Add-in by building the OutlookStartupMonitorSetup project,     // right click the project in the Solution Explorer, then choose install.    #endregion

    /// <summary>    /// The object for implementing an Add-in.    /// </summary>    /// <seealso class='IDTExtensibility2' />    [GuidAttribute("E2BAED2C-4B20-479F-A874-611B2AA35AD2"), ProgId("OutlookStartupMonitor.Connect")]    public class Connect : Object, Extensibility.IDTExtensibility2    {        /// <summary>        /// Implements the constructor for the Add-in object.        /// Place your initialization code within this method.        /// </summary>        public Connect()        {        }

        /// <summary>        /// Implements the OnConnection method of the IDTExtensibility2 interface.        /// Receives notification that the Add-in is being loaded.        /// </summary>        /// <param term='application'>        /// Root object of the host application.        /// </param>        /// <param term='connectMode'>        /// Describes how the Add-in is being loaded.        /// </param>        /// <param term='addInInst'>        /// Object representing this Add-in.        /// </param>        /// <seealso class='IDTExtensibility2' />        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)        {            applicationObject = application;            addInInstance = addInInst;        }

        /// <summary>        /// Implements the OnDisconnection method of the IDTExtensibility2 interface.        /// Receives notification that the Add-in is being unloaded.        /// </summary>        /// <param term='disconnectMode'>        /// Describes how the Add-in is being unloaded.        /// </param>        /// <param term='custom'>        /// Array of parameters that are host application specific.        /// </param>        /// <seealso class='IDTExtensibility2' />        public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)        {        }

        /// <summary>        /// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.        /// Receives notification that the collection of Add-ins has changed.        /// </summary>        /// <param term='custom'>        /// Array of parameters that are host application specific.        /// </param>        /// <seealso class='IDTExtensibility2' />        public void OnAddInsUpdate(ref System.Array custom)        {        }

        /// <summary>        /// Implements the OnStartupComplete method of the IDTExtensibility2 interface.        /// Receives notification that the host application has completed loading.        /// </summary>        /// <param term='custom'>        /// Array of parameters that are host application specific.        /// </param>        /// <seealso class='IDTExtensibility2' />        public void OnStartupComplete(ref System.Array custom)        {            try            {                Process olProc = Process.GetCurrentProcess();

                string sSource;                string sLog;                string sEvent;

                sSource = "Outlook Startup Monitor";                sLog = "Application";                sEvent = "Outlook was started at " + olProc.StartTime.ToString() + " and it finished loading add-ins at " + DateTime.Now.ToString();

                if (!EventLog.SourceExists(sSource))                {                    EventLog.CreateEventSource(sSource, sLog);                }

                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Information, 1);

            }            catch (Exception ex)            {

            }        }

        /// <summary>        /// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.        /// Receives notification that the host application is being unloaded.        /// </summary>        /// <param term='custom'>        /// Array of parameters that are host application specific.        /// </param>        /// <seealso class='IDTExtensibility2' />        public void OnBeginShutdown(ref System.Array custom)        {        }

        private object applicationObject;        private object addInInstance;    }}

I am also attaching the Visual Studio solution.

 

Comments are closed.