ArcObjects Library Reference  

LogExtension

About the Extension to listen to document open and save events Sample

[C#]

LogExtension.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;

namespace OpenSaveLogExtensionCS
{
    /// <summary>
    /// Simple extension that logs message when document is opened and saved
    /// </summary>
    [Guid("7d868fd7-f986-4347-bc93-b79751e12def")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("OpenSaveLogExtensionCS.LogExtension")]
    public class LogExtension : IExtension, IPersistVariant
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxExtension.Register(regKey);
            GMxExtensions.Register(regKey);
            SxExtensions.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxExtension.Unregister(regKey);
            GMxExtensions.Unregister(regKey);
            SxExtensions.Unregister(regKey);

        }

        #endregion
        #endregion
        private IApplication m_application;

        #region "Add Event Wiring for Open Documents"
        // Event member variables
        private IDocumentEvents_Event m_docEvents;

        // Wiring
        private void SetUpDocumentEvent(IDocument myDocument)
        {
            m_docEvents = myDocument as IDocumentEvents_Event;
            m_docEvents.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(OnOpenDocument); 

            //Optional, new and close document events
            m_docEvents.NewDocument += new IDocumentEvents_NewDocumentEventHandler(OnNewDocument);
            m_docEvents.CloseDocument += new IDocumentEvents_CloseDocumentEventHandler(OnCloseDocument);
        }

        void OnOpenDocument()
        {
            System.Diagnostics.Debug.WriteLine("Open Document", "Sample Extension (C#)");
            string logText = "Document '" + m_application.Document.Title + "'"
                            + " opened by " + Environment.UserName
                            + " at " + DateTime.Now.ToLongTimeString();

            LogMessage(logText);
        }

        void OnCloseDocument()
        {
            System.Diagnostics.Debug.WriteLine("Close Document", "Sample Extension (C#)");
        }
        void OnNewDocument()
        {
            System.Diagnostics.Debug.WriteLine("New Document", "Sample Extension (C#)");
        }
        #endregion

        #region "IExtension Implementations"
        public string Name
        {
            get
            {
                return "OpenSaveLogExtensionCS";
            }
        }

        public void Shutdown()
        {
            m_docEvents = null;
            m_application = null;
        }

        public void Startup(ref object initializationData)
        {
            m_application = initializationData as IApplication;
            SetUpDocumentEvent(m_application.Document);
        }
        #endregion

        #region "IPersistVariant Implementations"
        public UID ID
        {
            get
            {
                UID extUID = new UIDClass();
                extUID.Value = GetType().GUID.ToString("B");
                return extUID;
            }
        }

        public void Load(IVariantStream Stream)
        {
            Marshal.ReleaseComObject(Stream);
        }

        public void Save(IVariantStream Stream)
        {
            System.Diagnostics.Debug.WriteLine("Save Document", "Sample Extension (C#)");
            LogMessage("Document '" + m_application.Document.Title + "'"
                            + " saved by " + Environment.UserName
                            + " at " + DateTime.Now.ToLongTimeString());

            Marshal.ReleaseComObject(Stream);
        }
        #endregion

        private void LogMessage(string message)
        {
            m_application.StatusBar.set_Message(0, message);
        }
    }
}

[Visual Basic .NET]

LogExtension.vb

Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.esriSystem
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI

<ComClass(LogExtension.ClassId, LogExtension.InterfaceId, LogExtension.EventsId), _
 ProgId("OpenSaveLogExtensionVB.LogExtension")> _
Public Class LogExtension
    Implements IExtension
    Implements IPersistVariant
#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)

        'Add any COM registration code after the ArcGISCategoryRegistration() call

    End Sub

    <ComUnregisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)

        'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

    End Sub

#Region "ArcGIS Component Category Registrar generated code"
    ''' <summary>
    ''' Required method for ArcGIS Component Category registration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        GMxExtensions.Register(regKey)
        MxExtension.Register(regKey)
        SxExtensions.Register(regKey)

    End Sub
    ''' <summary>
    ''' Required method for ArcGIS Component Category unregistration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        GMxExtensions.Unregister(regKey)
        MxExtension.Unregister(regKey)
        SxExtensions.Unregister(regKey)

    End Sub

#End Region
#End Region

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "8545752a-d04c-4d92-84cd-65a5dd5f8de8"
    Public Const InterfaceId As String = "fdd2bdab-568d-490c-9ca3-916195a88ad2"
    Public Const EventsId As String = "07ba5ee7-1796-446f-947f-ad342153d3d4"
#End Region

    Private m_application As IApplication

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

#Region "Add Event Wiring for Open Documents"
    'Event member variables
    Private m_docEvents As IDocumentEvents_Event

    'Wiring
    Private Sub SetUpDocumentEvent(ByVal myDocument As IDocument)
        m_docEvents = CType(myDocument, IDocumentEvents_Event)
        AddHandler m_docEvents.OpenDocument, AddressOf OnOpenDocument

        'Optional, new and close events
        AddHandler m_docEvents.NewDocument, AddressOf OnNewDocument
        AddHandler m_docEvents.CloseDocument, AddressOf OnCloseDocument
    End Sub

    Private Sub OnOpenDocument()
        Debug.WriteLine("Open document", "Sample Extension (VB.Net)")

        Dim logText As String = "Document '" + m_application.Document.Title + "'" _
                            + " opened by " + Environment.UserName _
                            + " at " + DateTime.Now.ToLongTimeString()
        LogMessage(logText)
    End Sub

    Private Sub OnNewDocument()
        Debug.WriteLine("New document", "Sample Extension (VB.Net)")
    End Sub

    Private Sub OnCloseDocument()
        Debug.WriteLine("Close document", "Sample Extension (VB.Net)")
    End Sub
#End Region

#Region "IExtension Implementations"
    Public ReadOnly Property Name() As String Implements ESRI.ArcGIS.esriSystem.IExtension.Name
        Get
            Return "OpenSaveLogExtensionVB"
        End Get
    End Property

    Public Sub Shutdown() Implements ESRI.ArcGIS.esriSystem.IExtension.Shutdown
        m_docEvents = Nothing
        m_application = Nothing
    End Sub

    Public Sub Startup(ByRef initializationData As Object) Implements ESRI.ArcGIS.esriSystem.IExtension.Startup
        m_application = TryCast(initializationData, IApplication)
        SetUpDocumentEvent(m_application.Document)
    End Sub
#End Region

#Region "IPersistVariant Implementations"
    Public ReadOnly Property ID() As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.esriSystem.IPersistVariant.ID
        Get
            Dim extUID As New UIDClass()
            extUID.Value = Me.GetType().GUID.ToString("B")
            Return extUID
        End Get
    End Property

    Public Sub Load(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Load
        Marshal.ReleaseComObject(Stream)
    End Sub

    Public Sub Save(ByVal Stream As ESRI.ArcGIS.esriSystem.IVariantStream) Implements ESRI.ArcGIS.esriSystem.IPersistVariant.Save
        Debug.WriteLine("Save document", "Sample Extension (VB.Net)")
        LogMessage("Document '" + m_application.Document.Title + "'" _
                            + " saved by " + Environment.UserName _
                            + " at " + DateTime.Now.ToLongTimeString())
        Marshal.ReleaseComObject(Stream)
    End Sub
#End Region

    Private Sub LogMessage(ByVal message As String)
        m_application.StatusBar.Message(0) = message
    End Sub
End Class