Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (2)
Collapse All/Expand All Collapse All
SPDiagnosticsServiceBase Class

Provides a diagnostic logging category manager for Microsoft SharePoint Foundation.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public MustInherit Class SPDiagnosticsServiceBase _
    Inherits SPService _
    Implements IBackupRestoreConfiguration, IBackupRestore
Visual Basic (Usage)
Dim instance As SPDiagnosticsServiceBase
C#
[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.

C#
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.

C#
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();
        }
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Correction code sample      Pieter_Veenstra   |   Edit   |   Show History
SPDiagnosticsCategory cat = new SPDiagnosticsCategory (“SP Provisioning”, TraceSeverity.High, TraceSeverity.Verbose);

Should be
SPDiagnosticsCategory cat = new SPDiagnosticsCategory (“SP Provisioning”, TraceSeverity.High, EventSeverity.Verbose);
Tags What's this?: Add a tag
Flag as ContentBug
How can you use SPContext.Current within a console application?      Akif   |   Edit   |   Show History
How can you use SPContext.Current within a console application?
Tags What's this?: Add a tag
Flag as ContentBug
Correction: Second Code Sample Part 2      Siew Moi Khor - MSFT   |   Edit   |   Show History
Sorry about the formatting issue in the code sample. The community content editor messed up the formatting after I submitted the content. My team will update and republish this topic as soon as possible.
Tags What's this?: Add a tag
Flag as ContentBug
Correction: Second Code Sample      Siew Moi Khor - MSFT   |   Edit   |   Show History

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();
}

}

}


Tags What's this?: Add a tag
Flag as ContentBug
C# code sample doesn't compile      lisaa ... Suhaib.Khan   |   Edit   |   Show History
DiagnosticsService.Local should be SPDiagnosticsService.Local.

DiagnosticsService myULS = DiagnosticsService.Local;

Here DiagnosticsService.Local is ok as they already defined DiagnosticsService class under Contoso.Diagnostics namespace.
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker