In the previous article, we summarized how to enable the developer dashboard. With the developer dashboard, we can get a lot of information, which is very helpful to our SharePoint administrators and developers, such:
• Which controls are loaded?
• Loading time of each control
• Database Query and execution time
• Events triggered by page loading
• Loading time of each page stage
I. When to use spmonitoredscope
Developers can use spmonitoredscope in the following scenarios:
• Determine resource overuse.
• Identify performance bottlenecks.
• Determine how certain components interact with other components.
2. Limitations of spmonitoredscop
note that spmonitoredscope has some restrictions:
• only calls to the SharePoint database are captured.
• only Code packaged with spmonitoredscope on the front-end web server will appear on the developer dashboard. The Code executed on the Program server only displays spmonitoredscope information in the ULS log on the computer on which the Code is run.
• the code in sandsolution is invalid. You can only use spmonitorscope in full trust's solution.
3. How to Use spmonitorscope
1. Common usage
The spmonitoredscope class resides in the Microsoft. Sharepoint. Utilities namespace.
Spmonitoredscope is easy to use. Developers only need to "Wrap" the code segment to be monitored. Then, when executing the code, the system will write the measurement statistics into the ULS log and developer dashboard. In this way, developers and system administrators can immediately use the information about the components and the page on which the components are located. Of course, such "packaging" can be nested.
Next, we create a webpart. In its pageload event, we use spmonitoredscope to nest two methods: fastmethod and slowmethod (the two methods respectively use spmonitoredscope ), the common usage here is: we use the first constructor of the spmonitorscope class. This constructor only imports one string type parameter (that is, the name you give to the scope), for example:
Using ( New Spmonitoredscope ("My scope name "))
{
Dosomework ();
}
The Execution Code in webpart is as follows:
Protected Void Page_load ( Object Sender, eventargs E)
{
Using(Spmonitoredscope scope =NewSpmonitoredscope ("Calltwomethods"))
{
Slowmethod ();
Fastmethod ();
}
}
Private Void Slowmethod ()
{
Using (Spmonitoredscope scope = New Spmonitoredscope ( " Slowmethod " ))
{
System. Threading. thread. Sleep (1200 );
}
}
Private Void Fastmethod ()
{
Using (Spmonitoredscope scope = New Spmonitoredscope ( " Fastmethod " ))
{
System. Threading. thread. spinwait ( 10 );
}
}
The execution result is as follows:
2. Other usage methods (monitor the usage of other resources and use the performance threshold)
We need the second constructor of the spmonitoredscope class to implement this usage. This constructor has three parameters.
Spmonitoredscope (string, uint32, ispscopedperformancemonitor [])
Public Spmonitoredscope (
String Name,
Uint Maximumexecutiontime,
Params Ispscopedperformancemonitor [] monitors
)
The first parameter is the same as above (the name we get for scope)
The second parameter is the maximum performance threshold (in milliseconds) we set for monitoring operations)
The third parameter is a group of array objects that implement the ispscopedperformancemonitor interface. The following table lists several classes that implement this interface.
Of course, with this interface, we can not only add the related classes listed in the above table to the third parameter array of spmonitoredscope constructor, we can also create several custom monitors to monitor the resources we are interested in, and add this customer monitor to the current spmonitoredscope. Therefore, the third parameter can also be a list of custom monitor objects.
The following code uses the classes listed in the above table to construct our spmonitoredscope object.
Using (
Spmonitoredscope ( " My scope name " , 1000 ,
New Sprequestusagecounter ( 3 ),
New Spsqlquerycounter ()))
{
Thread. Sleep ( 5000 );
}
When the execution time of our monitoring Code exceeds our set threshold (1000), the developer dashboard's border turns red and increases the trace level. Example 3
Sometimes you need not only to record the scope of monitoring to the developer dashboard, but also want to know how many times the scope code of your monitor has been executed. For example, if your system uses the Web service provided by a third-party company, this service is charged based on the number of visits. You can record the number of times you use this WebService, once the number of visits exceeds your set threshold, you can set the relevant return value (valueisexcessive ). Before each scope ends and spmonitoredscope object is released (disposed), it checks all the monitors (that is, each monitor object in the monitor list in the third parameter) and their valueisexcessive values, if this value is true, logs will be logged to ULS.
Here, we need to write a user-defined monitor object, which inherits the self-interface ispscopedperformancemonitor. The Code is as follows:
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; // Define a static variable to store usage
Static Customermonitor ()
{
S_times = 0 ;
}
Public Customermonitor ()
{
S_times ++; // The cumulative number of usage. Each time you call this constructor, add one
}
Public Void Dispose (){}
// For thread-safe reasons, description and rendervalueforweb can only be used by Sharepoint managed monitors, and they cannot be used by custom application code.
Public String Description
{
Get { Return " The Monitor " ;}
}
// The name attribute is used when logging into to the trace log.
Public String Name
{
Get {Return " Themonitor " ;}
}
Public String Rendervalueforweb ()
{
Return String . Format ( " Monitor: {0} " , S_times );
}
// Value is used to return the current s_times record value.
Public Object Value
{
Get { Return S_times ;} // Returns the actual number of times used.
}
// Valueisexcessive is used to indicate whether s_times has exceeded the specified limit.
Public Bool Valueisexcessive
{
Get { Return S_times> 5 ;} // Check whether the specified number of times is exceeded
}
}
}
The code for calling it is as follows:
Using (Spmonitoredscope scope =
New Spmonitoredscope ( " Custom Scope " , 100 , New Custommonitor ())){
// ...
}
If you run the above method, when your custom monitor monitors more than 5 accesses, it will immediately start the log record and write the log record into the ULS logs, you can find the log record about it in ULS. Otherwise, it will not be recorded in the ULS log, that is, you do not need to care about it, because it does not exceed the limit you set.
Note that if you set the monitoring category item in SharePoint Foundation> to verbose, even if your custom monitor monitors the number of visits "not" more than 5 times, the system will start the log record, that is, the system will record all behaviors in all the monitors, whether or not this behavior exceeds the limit you set.
In the developer dashboard configuration, there is also an attribute called autolaunch. if you set the dashboard mode to on demand and auto launch to true, the developer dashboard will be automatically displayed when any critical event (defined in shaerpoint) is written into the ULS log.
Another interesting thing is that someone found on the internet that spmonitorscope cannot be used in sandboxed solutions according to Microsoft's statement. However, according to the msdn instructions, the ispscopedperformancemonitor interface can be used in sandboxed solutions, I don't know who can explain this situation.
Iv. Best Operation suggestions
According to Microsoft's suggestion, we should use spmonitoredscope to encapsulate all calls to external components (such as custom databases and external web services. This allows administrators to easily identify these components as fault points and quickly identify problems.