Consume a Service & Activity Logs Writing

How to: Consume a Service (C#)
Any managed VSPackage that derives from Package and that has been correctly sited can ask itself for any global service. Because the Package class implements IServiceProvider, any VSPackage that derives from Package is also a service provider.
When Visual Studio loads a managed VSPackage, it passes an IServiceProvider service provider to the Package SetSite method during initialization, siting the VSPackage. The Package class wraps this service provider and provides the GetService method for obtaining services.
Because the VSPackage constructor is called before the VSPackage is sited, global services are typically unavailable from within the VSPackage constructor.
How to: Troubleshoot Services for a workaround.
To consume a service
Insert this code in any method except the VSPackage constructor:
IVsActivityLog log =
GetService(typeof(SVsActivityLog)) as IVsActivityLog;
if (log == null) return;

This code obtains an SVsActivityLog service and casts it to an IVsActivityLog interface, which can be used to write to the activity log.


How to: Write to the Activity Log (C#) :
To write an entry to the activity log
Insert this code in the Initialize method or in any other method except the VSPackage constructor:
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.
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.
Load the VSPackage.
To examine the activity log
Find the activity log.
By default, the location is \Application Data\Microsoft\VisualStudio\8.0Exp\ActivityLog.XML.
Open the activity log with any text editor.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.