0

We have creates a webpart and in the catch block we are writing logs to uls.

My concern is regarding permission to write to this log. Will users with read-only access to sharepoint face no issue?

 private void WriteToULS(TraceSeverity Level, string OutStr)
        {
            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Portal Error", Level, EventSeverity.Error), TraceSeverity.Unexpected, OutStr, null);
        }

I would also like to know if it a good practice to use:

private void WriteToULS(TraceSeverity Level, string OutStr)
            {
 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Portal Error", Level, EventSeverity.Error), TraceSeverity.Unexpected, OutStr, null);
 }); 
            }

in the code for all webparts.

2 Answers 2

1

Call the below method ,this will ensure you are have correct/appropriate permissions while users with read permissions write to log file.

SPSecurity RunWithElevatedPriviledges{
LogData();
}

public static void LogData(string categoryName, Level type, string section, string message, string exception)
{
    try
    {
        /*Log SharePoint*/
        SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
        diagnosticsService.WriteTrace(0,
                                      new SPDiagnosticsCategory(categoryName, TraceSeverity.Unexpected,
                                                                EventSeverity.Error),
                                      TraceSeverity.Unexpected, exception, exception);
    }
    catch
    {
        throw new Exception("ExceptionManager.LogData > Failed !");
    }
}

Refer for ULS logs details if required : where see the log SPDiagnosticsService WriteTrace

6
  • This is what I mentioned :-) Apr 5, 2014 at 17:37
  • lol , i didnt see your comment :)
    – Nikhil J
    Apr 5, 2014 at 22:07
  • Hi Nikhil, I tested it out and it seems that the user with only read permission can write to ULS log, it didnt give me any error. I am logging to ULS at every catch block in my webpart code..
    – variable
    Apr 7, 2014 at 4:42
  • You logged in with user with read access . But SPDiagnosticService will/always use app pool account to write to ULS logs.
    – Nikhil J
    Apr 7, 2014 at 15:07
  • Ok, can you tell me what priviledges should the apppool account (site collection administrator) have if I donot use elevated priviledges?
    – variable
    Apr 8, 2014 at 12:44
0

Users with read only access won't be able to write to ULS. Write the code inside RunWithElevatedPreviliges delegate

2
  • I have user with read only permission and I tested it out and can confirm that the user can write to ULS log without using ElevatedPriviledges.. Can you confirm this?:
    – variable
    Apr 7, 2014 at 4:41
  • Also, the RunWithElevatedPreviliges code runs under the account used during webapplication creation. So unless that user has permission to write to ULS then it will throw error?
    – variable
    Apr 10, 2014 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.