Programmatically logging to the Sharepoint ULS

后端 未结 5 1682
一整个雨季
一整个雨季 2020-12-30 14:08

I\'d like to log stuff in my Sharepoint Web Parts, but I want it to go into the ULS. Most examples that I\'ve found log into the Event Log or some other file, but I did not

5条回答
  •  灰色年华
    2020-12-30 14:48

    Try Below Code: (add this reference: using Microsoft.SharePoint.Administration;)

    try
            {
    
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
                    diagSvc.WriteTrace(123456, new SPDiagnosticsCategory("Category_Name_Here", TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable, "{0}:{1}", new object[] { "Method_Name", "Error_Message"});
                });
            }
            catch (Exception ex)
            {
            }
    

    Now open uls viewer and filter by your category name.

提交回复
热议问题