다음을 통해 공유


Creating a Custom Diagnostics Service

Applies to: SharePoint Foundation 2010

This article provides a sample of how to create a diagnostics service to allow a developer to use the improved ULS API shipped in Microsoft SharePoint Foundation 2010.

Custom Diagnostic Service

A custom diagnostics service must inherit from the SPDiagnosticsServiceBase found in the Microsoft.SharePoint.Administration namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Internal.Diagnostics;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
namespace SampleDiagnosticServices
{
    // always create your own Guid attribute when deriving from ULSDiagnosticsServiceBase
    // The Guid attribute is required by the WSS ConfigDB
    [Guid("5C622C71-A6F8-4527-B05B-4D7ED4A69760")]
    public class O14ULS : ULSDiagnosticsServiceBase
    {
        public O14ULS()
        {
        }
        public O14ULS(string name, SPFarm parent)
            : base(name, parent)
        {
        }
        public static O14ULS Local
        {
            get { return SPDiagnosticsServiceBase.GetLocal<O14ULS>(); }
        }
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            // The "0, 0, false" parameters are NOT optional when using ULSDiagnosticsServiceBase.

            yield return new SPDiagnosticsArea("ULS Area 1", 0, 0, false,
                new List<SPDiagnosticsCategory>()
                {
                    new SPDiagnosticsCategory("Category 1", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 2", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 3", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 4", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 5", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 6", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                }
            );
            yield return new SPDiagnosticsArea("ULS Area 2",
                new List<SPDiagnosticsCategory>()
                {
                    new SPDiagnosticsCategory("Category 1", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 2", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 3", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 4", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 5", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                    new SPDiagnosticsCategory("Category 6", TraceSeverity.Verbose, EventSeverity.Verbose, 0, 0, false),
                }
            );
        }

Using localized strings for log messages

It may be necessary sometimes to use localized strings for messages that are relevant to the culture where Microsoft SharePoint Foundation 2010 is installed. In order provide localized messages in the administrative UI, you will need to override the ResourceDll() method and have it provide a path to your custom dll.

The following method will provide the diagnostic service with the dll location

protected override string ResourceDll
        {
            get
            {
                // optional: the developer could provide the path to an actual resource dll to get localized names
                return null;
            }
        }

Registering the Diagnostics Service with SharePoint Foundation

In order for SharePoint Foundation 2010 to use the Diagnostics service you have created, you must register it. Before this is done however, each web front end in the SharePoint Foundation 2010 farm must have the assembly installed and registered in the Global Assembly Cache (GAC).

Once the assembly is installed, you can register the service with SharePoint by creating a registry key, or using a Windows PowerShell command.

Registry Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\WSS\Services

PS C:\<AssemblyLocation> [void][System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.Office.Server’)
[Microsoft.Office.Server.Administration.DiagnosticsService]::Local.Update()

See Also

Concepts

Writing to the Trace Log from Custom Code

Overview of Unified Logging System (ULS) Logging