共用方式為


SPDiagnosticsServiceBase Class

Provides a diagnostic logging category manager for Microsoft SharePoint Foundation.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.Administration.SPPersistedObject
      Microsoft.SharePoint.Administration.SPPersistedUpgradableObject
        Microsoft.SharePoint.Administration.SPService
          Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase
            Microsoft.SharePoint.Administration.SPDiagnosticsService

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public MustInherit Class SPDiagnosticsServiceBase _
    Inherits SPService _
    Implements IBackupRestoreConfiguration, IBackupRestore
'Usage
Dim instance As SPDiagnosticsServiceBase
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public abstract class SPDiagnosticsServiceBase : SPService, 
    IBackupRestoreConfiguration, IBackupRestore

Remarks

The SPDiagnosticsServiceBase class is an abstract base class that a third party developer can inherit from when writing a concrete subclass to provide custom diagnostic categories and to use the WriteTrace and WriteEvent methods to perform logging. For more information, see Using the Trace Logging API.

Examples

The following example shows one implementation of a diagnostic service. Note that the values of the CategoryId enumeration as well as the categories created by the ProvideAreas method are arbitrary and used only for the purpose of illustration. You can create categories to suit your own purposes.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

namespace Contoso.Diagnostics
{
    public enum CategoryId
    { 
        None = 0,
        Deployment = 100,
        Provisioning = 200,
        CustomAction = 300,
        Rendering = 400,
        WebPart = 500
    }

    [System.Runtime.InteropServices.GuidAttribute("DBEEB5AB-C5A7-46B5-A2BB-5581F960C333")]
    class DiagnosticsService:SPDiagnosticsServiceBase
    {
        private static string DiagnosticsAreaName = "Contoso";

        public DiagnosticsService()
        { 
        }

        public DiagnosticsService(string name, SPFarm farm)
            :base(name, farm)
        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsCategory> categories = new List<SPDiagnosticsCategory>();
            foreach (string catName in Enum.GetNames(typeof(CategoryId)))
            {
                uint catId = (uint)(int)Enum.Parse(typeof(CategoryId), catName);
                categories.Add(new SPDiagnosticsCategory(catName, TraceSeverity.Verbose, EventSeverity.Error, 0, catId));
            }

            yield return new SPDiagnosticsArea(DiagnosticsAreaName, categories);
        }

        public static DiagnosticsService Local
        {
            get 
            {
                return SPDiagnosticsServiceBase.GetLocal<DiagnosticsService>();
            }
        }

        public SPDiagnosticsCategory this[CategoryId id]
        {
            get
            {
                return Areas[DiagnosticsAreaName].Categories[id.ToString()];
            }
        }
    }
}

The following code sample shows how you can use the diagnostics service to write an entry in the trace log.

Sample code provided by: James Fort, Microsoft Corporation.

using System;
using Microsoft.SharePoint.Administration;

namespace Contoso.Diagnostics
{
    class Program
    {
        static void Main(string[] args)
        {
            DiagnosticsService myULS = new DiagnosticsService (“SP Provisioning”, SPContext.Current.Site.WebApplication.Farm);
            if (myULS != null)
            {
                SPDiagnosticsCategory cat = new SPDiagnosticsCategory (“SP Provisioning”, TraceSeverity.High, TraceSeverity.Verbose);
                cat = myULS[CategoryId.Provisioning];
                string format = "Tracing test of {0} service";
                myULS.WriteTrace(1, cat, TraceSeverity.Verbose, format, myULS.TypeName);
            }
            Console.ReadLine();
        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPDiagnosticsServiceBase Members

Microsoft.SharePoint.Administration Namespace

Other Resources

Trace Logs

Using the Trace Logging API