SharePoint Study Notes-debug-use ULS log to track solution error messages

Source: Internet
Author: User
When developing SharePoint solution, we can use attach to process to debug our solution. However, once we deploy solution on the production machine, it is difficult for us to use this most direct method, if a solution error occurs, we need sufficient means to obtain the detailed error information as far as possible) this provides us with a way to help us locate User tracking information. In the early release of sharepoint2007, although ULS was also released together, we could not use it, which was clearly stated in the sharepoint2007 SDK and was only used internally. In SharePoint 2010, all of this is changed. Now we can use it in our code to write the tracing information we need to capture.
Create a Sharepoint project and create a visual web part in the project.

Write in the button's click background code (Microsoft. Sharepoint. administration is required)

Protected void btnlogin_click (Object sender, eventargs E)
{
Try {var I = 0; var A = 2/I ;}
Catch (exception ex) {loggingservice. logerror ("webparts", Ex. Message );}
}

Built this project is deployed to the test website, and then run the button click event on the webpart, an error message will be written to the ULS log (in the directory

\ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 14 \ logs. By default, the trace log name consists of the computer name, date, and time information, the file type is "Text Document ".), Generally, you can use SharePoint ULS Log Viewer to open the corresponding log file and you can see the following error event records (you can also use the ulsviewer provided by Microsoft to view them. Its usage is somewhat different ):

You will notice that in the open log file, the area value of the corresponding button event error record is unkown. If we have multiple solutions deployed on the server, this makes it difficult for us to identify the solution registration error. Therefore, we need to further improve our practice. Therefore, create a new class in the original project that inherits from the spdiagnosticsservicebase base class, for example

The definition code of this class is as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Microsoft. Sharepoint. Administration; // needs to be introduced

Namespace copylistcontent
{
Public class loggingservice: spdiagnosticsservicebase
{
Public static string mydiagnosticareaname = "mydiagnostictest ";
Private Static loggingservice _ current;
Public static loggingservice current
{
Get
{
If (_ current = NULL)
{_ Current = new loggingservice ();}
Return _ current;
}
}

Private loggingservice ()
: Base ("mydiagnostictest logging service", spfarm. Local)
{}

Protected override ienumerable <spdiagnosticsarea> provideareas ()
{
List <spdiagnosticsarea> areas = new list <spdiagnosticsarea> {
New spdiagnosticsarea (mydiagnosticareaname, new list <spdiagnosticscategory>
{New spdiagnosticscategory ("webparts", traceseverity. Unexpected, eventseverity. Error )})};
Return areas;
}

Public static void logerror (string categoryname, string errormessage)
{
Spdiagnosticscategory Category = loggingservice. Current. areas [mydiagnosticareaname]. categories [categoryname];
Loggingservice. Current. writetrace (0, category, traceseverity. Unexpected, errormessage );
}
}

}

In this class, we have written our current solution Identification name "mydiagnostictest" in the area, re-modified the button event definition, and referenced the class we defined above to write the ULS log.

Protected void btnlogin_click (Object sender, eventargs E)
{
Try {var I = 0; var A = 2/I ;}
Catch (exception ex)
{Spdiagnosticsservice. Local. writetrace (0,
New spdiagnosticscategory ("My category", traceseverity. Unexpected, eventseverity. Error ),
Traceseverity. Unexpected, Ex. Message, Ex. stacktrace );
}
}

Re-built solution and deploy and run it. Return to the ULS log and use the SharePoint ULS Log Viewer to view it. You can see the following:

We can identify the area field to easily find the error message written by our solution. Of course, you can also flexibly organize the content you need to write so that you can easily locate the User tracking information of solution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.