SPDiagnosticsServiceBase 类
Microsoft SharePoint Foundation提供的诊断日志记录类别管理器。
继承层次结构
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
命名空间: Microsoft.SharePoint.Administration
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public MustInherit Class SPDiagnosticsServiceBase _
Inherits SPService _
Implements IBackupRestoreConfiguration, IBackupRestore
用法
Dim instance As SPDiagnosticsServiceBase
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public abstract class SPDiagnosticsServiceBase : SPService,
IBackupRestoreConfiguration, IBackupRestore
备注
SPDiagnosticsServiceBase类是一个抽象基类,给第三方开发人员可以从继承,编写具体的子类,以提供自定义诊断类别并在WriteTrace和WriteEvent方法用于执行日志记录时。有关详细信息,请参阅Using the Trace Logging API。
示例
下面的示例演示一种诊断服务的实现。请注意CategoryId枚举以及ProvideAreas方法创建的类别的值是任意的和使用只为了图。您可以创建的类别以满足自己的目的。
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()];
}
}
}
}
下面的代码示例演示如何使用诊断服务,若要在跟踪日志中写入条目。
Sample code provided by:James Fort,微软公司。
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();
}
}
}
线程安全性
该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。
另请参阅
引用
Microsoft.SharePoint.Administration 命名空间