Share via


Logging an Event to Operations

Typical Goals

In this scenario, you want to record an event to both the Windows event log and the ULS trace log. The record of the event is meant to be used by system administrators.

Solution

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.

Logging an Event

The following code demonstrates how to log an event.

ILogger logger = SharePointServiceLocator.Current.GetInstance<ILogger>();
logger.LogToOperations("The current user doesn’t have a PartnerID specified.", 
                       (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 use enumerated values or constants for your application's event IDs.

Usage Notes

You should use the LogToOperations method to log information that is meaningful to system administrators. If possible, messages should indicate an action that the administrator can take. For example, compare the message "A timeout occurred" to "Could not connect to database X, because the connection timed out". The first message does not provide any information to system administrators that they can respond to. The second message gives them an idea of where to look for the problem.

If the event is caused by an exception, consider using the overloaded version of the LogToOperations method that takes an exception as an argument. This method formats and logs the exception details.

The LogToOperations method has overloaded versions that allow you to specify optional information for the event you are recording. You can include the following:

  • An event-log entry type that distinguishes warnings from errors
  • A string that denotes a category

The following are examples of event logging that use the different overloads of 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. 
logger.LogToOperations(msg);

// 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,
                       EventLogEntryType.Error);

// 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 to Operations.

For more information about the EventLogEntryType enumeration, see EventLogEntryType Enumeration on MSDN.

Home page on MSDN | Community site