Share via


How to: Write to the Activity Log

VSPackages can write messages to the activity log. This feature is especially useful for debugging VSPackages in retail environments.

To write an entry to the activity log

  1. Insert this code in the Initialize method or in any other method except the VSPackage constructor:

    Dim log As IVsActivityLog = _
        TryCast(Me.GetService(GetType(SVsActivityLog)), IVsActivityLog)
    
    If (log Is Nothing) Then Return
    
    Dim hr As Integer = log.LogEntry( _
        CUInt(__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION), _
        Me.ToString(), _
        String.Format(CultureInfo.CurrentCulture, _
            "Entering initializer for: {0}", Me.ToString()))
    
    IVsActivityLog log = GetService(typeof(SVsActivityLog)) as IVsActivityLog;
    if (log == null) return;
    
    int hr = log.LogEntry((UInt32)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION,
        this.ToString(),
        string.Format(CultureInfo.CurrentCulture,
        "Entering initializer for: {0}", this.ToString()));
    

    This code gets the SVsActivityLog service and casts it to an IVsActivityLog interface. LogEntry writes an informational entry into the activity log using the current cultural context.

  2. In the Visual Studio Command Window, start devenv.exe with the /log switch. Make sure that you add the appropriate /rootsuffix argument to run in the registry root where the VSPackage is registered, for example: devenv /rootsuffix Exp /log.

  3. Load the VSPackage.

To examine the activity log

  1. Find the activity log in the subfolder for Visual Studio data.

    For example, %AppData%\Microsoft\VisualStudio\10.0\ActivityLog.XML.

  2. Open the activity log with any text editor.

    Following is a typical entry:

    50  Entering initializer for: Company.MyApp.MyAppPackage ...
    

Robust Programming

Because the activity log is a service, the activity log is unavailable in the VSPackage constructor.

You should obtain the activity log just before writing to it. Do not cache or save the activity log for future use.

See Also

Tasks

How to: Troubleshoot VSPackages

Reference

IVsActivityLog

__ACTIVITYLOG_ENTRYTYPE

Other Resources

VSPackages