Provides a diagnostic logging category manager for Microsoft SharePoint Foundation.
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _ <SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _ Public MustInherit Class SPDiagnosticsServiceBase _ Inherits SPService _ Implements IBackupRestoreConfiguration, IBackupRestore
Dim instance As SPDiagnosticsServiceBase
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)] [SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)] public abstract class SPDiagnosticsServiceBase : SPService, IBackupRestoreConfiguration, IBackupRestore
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.
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(); } } }
Issue: The second code sample in this reference topic does not work. Corrected Version: Thanks to James Fort [MSFT] for correcting the code sample and for providing the following code sample. The code sample shows how you can use the diagnostics service to write an entry in the trace log.
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(); } }}