现在的位置: 首页 > 综合 > 正文

SharePoint【调试,诊错系列】– Developer Dashboard的使用(3.向Assert and Critical Events段插入信息)

2012年05月21日 ⁄ 综合 ⁄ 共 1303字 ⁄ 字号 评论关闭

 如果你开发了一个Webpart并把它呈现到Sharepoint网站上时,Sharepoint会创建一个top-level的scope,在这个Scope中,Sharepoint加入了一个名叫SPCriticalTraceCounter的Monitor,这个Monitor可以被用来向Developer Dashborad的Assert and Critical Events段插入信息。如下图:


 如果你点击信息条左边的链接,你就可以得到更多关于这个记录事件的信息内容(当前事件的调用堆栈)。如下图:


 如何向Developer Dashboard的这个Assert and Critical Events信息段中插入信息呢?我们可以使用SPCriticalCounter类的一个静态函数AddDataToScope来实现。

 当有相应的请求传来时,通常Sharepoint会自动加载这个SPCriticalCounter的Monitor,当然还有其它一些Monitor也会和它一起加载,SPCriticalCounter类定义如下

public sealed class SPCriticalTraceCounter : ISPScopedPerformanceMonitor, IDisposable, ISPPerformanceMonitor 

SPCriticalCounter是一个封闭类,它继承了ISPScopedPerformMonitor接口,这个接口我们在上一篇有介绍。
我们可以使用如下代码来添加用户的相关信息:

复制代码
using (SPMonitoredScope scope = new SPMonitoredScope("Customer Critical")) {
SPCriticalTraceCounter.AddDataToScope(
2010,
"Critical Error",
15,
"An Critical error occurred");
}
复制代码

AddDataToScope静态函数的定义如下

public static void AddDataToScope(uint tagID, string category, int traceLevel, string message); 

上面的定义代码中
 第一个参数是一个用户定义的标签值(custom tag id)用于标识记录项
 第二个参数是一个string类型,它是这个记录项的分类(category)
 第三个参数是一个int类型,它标识跟踪层次(trace level),通过Reflector分析我们可以看到,它对应于Microsoft.SharePoint.Diagnostics.ULSTraceLevel的枚举定义,相关值如下: 

复制代码
internal enum ULSTraceLevel
{
    High = 20,
    Medium = 50,
    Monitorable = 15,
    Unexpected = 10,
    Verbose = 100,
    VerboseEx = 200
}   
复制代码

 第四个参数是这个记录项真正的记录信息。

抱歉!评论已关闭.