using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Utilities;
namespace CopyListContent
{
public class CustomerMonitor : ISPScopedPerformanceMonitor
{
static int s_times; //定义一个静态变量来存放使用次数
static CustomerMonitor()
{
s_times = 0;
}
public CustomerMonitor()
{
s_times++; //累积使用次数,每次调用此构造函数时就加一
}
public void Dispose() { }
//出于thread-safe的原因,Description与RenderValueForWeb只能被Sharepoint managed monitors使用,它们不能被custom application代码使用。
public string Description
{
get { return "The Monitor"; }
}
//Name属性在logging into到Trace Log的时候使用
public string Name
{
get { return "TheMonitor"; }
}
public string RenderValueForWeb()
{
return string.Format("MONITOR: {0}", s_times);
}
//Value用于返回当前的s_times记录值
public object Value
{
get { return s_times; } //返回实际使用的次数
}
//ValueIsExcessive用于反映s_times 是否已经超过规定的限度
public bool ValueIsExcessive
{
get { return s_times > 5; } //检查是否超过设定的使用次数
}
}
} 调用它的代码如下:
using (SPMonitoredScope scope =
new SPMonitoredScope("Custom scope", 100, new CustomMonitor()))
{
// ...
} 如果你运行上述方法,当你的用户自定义Monitor监控到访问次数超过5次时,它就会立即启动log记录,把日志记录写入ULS logs中,你就可以到ULS中找到关于它的日志记录了。否则,它不会记录到ULS日志中,也即:你无需关心它,因为它没有超过你设置的限度。
这里还需要注意的是,如果你在SharePoint Foundation > Monitoring category 项设置为Verbose,那么即使你的用户自定义Monitor监控到访问次数"没有"超过5次时,系统还是会启动Log记录,也就是说,系统会记录所有的Monitor内的所有行为,不论此行为是否超出你设定的限度值。
在 Developer Dashboard的configuration中,还包含一个属性叫AutoLaunch. 如果你设置dashboard 模式为On Demand并且 Auto Launch为 true, 那么Developer Dashboard会在任何关键性事件(在Shaerpoint中定义)被写入ULS日志时都会自动显示出来。
还有一个有意思的地方是在网上有人发现,SPMonitorScope按微软的说法是不能用于sandboxed solutions的,但是根据MSDN的说明,ISPScopedPerformanceMonitor 接口却可以在sandboxed solutions 中使用,这种情况不知道谁能给个说明。
四、最佳操作建议 根据微软的建议,我们应使用 SPMonitoredScope 来包装对外部组件(如自定义数据库、外部 Web 服务等)进行的所有调用。这使得管理员能够轻松将这些组件识别为故障点,并快速找出问题。