Share via


Selecting a Custom Event Source Name

Typical Goals

By default, the SharePoint logger writes messages for IT administrators to the Windows event log using the Office SharePoint Server event source. You can change the event source used to log to the Windows event log by selecting a custom event source name in the configuration information used by the logger.

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 the SharePoint configuration manager using the IConfigManager interface. Invoke the SetInPropertyBag method and pass the custom event source name that you want to use for your events as an argument. Use the library-provided constant Microsoft.Practices.SPG.Common.Constants.EventSourceNameConfigKey as the configuration key argument.

Selecting a Custom Event Source Name

The following code demonstrates how to select a custom event source name for the Windows event log.

string eventSourceKey = 
             Microsoft.Practices.SPG.Common.Constants.EventSourceNameConfigKey;

IConfigManager configManager = 
             SharePointServiceLocator.Current.GetInstance<IConfigManager>();

configManager.SetInPropertyBag(eventSourceKey, 
                               "My Custom Event Source", 
                               SPFarm.Local);

The invocation of the SetInPropertyBag method sets a property of the current farm to the custom event source that is named My Custom Event Source. The SharePoint logger will use this event source when logging to the Windows event log.

The eventSourceKey variable has the value Microsoft.Practices.SPG.Common.EventSourceName.

Usage Notes

The event source used in combination with the event ID is often used by system administrators to identify an event that has occurred in the system. Using the Office SharePoint Server event source works, but is not a recommended practice because it mixes custom event messages with product event messages in the Windows event log. It is recommended that you use your own event source name. The SharePoint logger writes an additional warning message if the default event source is used for trace messages.

Home page on MSDN | Community site