This project is read-only.

Logging an Event

Typical Goals

In this scenario, you want to record an event to the Windows event log and the ULS trace log.


In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll. Use the SharePoint service locator to get a reference to a Microsoft.Practices.SPG.Common.Logging.ILogger interface and invoke the LogToOperations method. Pass an error string and an optional integer event ID to the LogToOperations method. For more information about the service locator, see The SharePoint Service Locator.


For an extended example of how to log an event using the SharePoint logger, see [TBD].

Logging an Event

The following code demonstrates how to log an event.
ILogger logger = SharePointServiceLocator.Current.GetInstance<ILogger>();
logger.LogToOperations("Lookup failed", (int) EventLogEventId.PartnerNotFound);

The LogToOperations method writes to both the Windows event log and to the ULS trace log. The EventLogEventId enumeration is an example of a user-defined set of event IDs. It is a recommended practice to encode your application's event types as enumerated values.

Usage Notes

The LogToOperations method has overloaded versions that allow you to specify optional information for the event you are recording. You can include:
  • An event-log entry type that distinguishes warnings from errors.
  • A string that denotes a category.
Here are examples of event logging using the LogToOperations method.
// ILogger logger = ...
// string msg = ...
// string category = ...

// log an event 
logger.LogToOperations(msg, (int) EventLogEventId.SkuNotFound);

// log an event without specifying an event ID 

// log an event giving an event-log entry type (without specifying an event ID)
logger.LogToOperations(msg, EventLogEntryType.Error);

// log an event with an event-log entry type
logger.LogToOperations(msg, (int) EventLogEventId.PartnerNotFound,

// log an event with an event-log entry type and category 
logger.LogToOperations(msg, (int) EventLogEventId.PartnerNotFound,
                       EventLogEntryType.Error, category)

You can also log exception information with your event. To do this, see Logging an Exception.

For more information on the EventLogEntryType enumeration, see <insert link>.

To give feedback.
Copyright (c) 2007 by Microsoft Corporation. All rights reserved.

Last edited Jun 16, 2009 at 8:49 PM by ckeyser, version 1


No comments yet.