Logging to ULS in SharePoint 2010.

ULS stands for Unified Logging Service
ULS helps developer to avoid debugging code again and again to find out bugs.
ULS clearly specifies the error in details.
Generally SharePoint logs can be found at hive folder i.e. 14LOGS
But this log file is not an easy task to read and understand.

To make it easy you should use tools like ULS viewer.
Download ULS viewer to read logs.

To write log in ULS need to write below code.

Please add 2 namespace:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                using (SPSite site = new SPSite(“http://myspsite:1985”))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[“mylist1”];
                        SPListItemCollection items = list.GetItems();
                        web.AllowUnsafeUpdates = true;
                        foreach (SPListItem item in items)
                        {
                            item[“Title”] = “new val1”;
                            item.Update();
                        }
                        web.AllowUnsafeUpdates = false;
                    }
                }
            }

            catch(Exception ex)
            {
                  SPDiagnosticsService spDiag = SPDiagnosticsService.Local;
                spDiag.WriteTrace(0,new SPDiagnosticsCategory(“My ULS Log”, TraceSeverity.Monitorable, EventSeverity.Error),
                                                        TraceSeverity.Monitorable,
                                                        “ULS log error:  {0}”,
                                                        new object[] { ex.Message });   

            }
        }

See output in ULS viewer:

Leave a Comment

RSS
YouTube
YouTube
Instagram