Remember to Elevate For Event Log Entries

Remember if you are logging errors to the system event log when developing your SharePoint application code you need to commonly promote the privileges that it is occurring under. Below is a common line that you can use in your catch blocks that gives you pretty much all the information you could want about an exception object:

[csharp] SPSecurity.RunWithElevatedPrivileges(delegate { EventLog.WriteEntry(Your WebPart Name, string.Concat(new object[] { “Exception in Function: “, ex.Message, ” Inner Excpetion: “, ex.InnerException, “Call Stack: “, ex.StackTrace }), EventLogEntryType.Error); });


6 thoughts on “Remember to Elevate For Event Log Entries”

  1. Presumably this is why we have had issues using Log4Net to consistently log to the system event logs too…

  2. So this assumes that you don’t want to write to the ULS logs? I’ve seen people mention doing that, but never heard of SharePoint apps writing to the system event log. I can see the obvious benefits of the event log over the ULS logs (ULS logs recycle QUICK), but would you recommend the system log categorically? Would you ever recommend writing to the ULS log?

    I’m just curious.

  3. Jim:

    Yup, I am pretty sure this is why we where having problems with it.


    There are indeed particular things that I choose to log to the ULS, it commonly depends on whom I expect to check for and ultimately settle the error. It is problematic to distinguish uniformly however because I believe that it breaks down to your overall project team structure, where responsibilities lie for the production environment, and organizational culture in aggregate Generally though, I try to reserve the event log for architectural events that usually I expect the operations end of the house to troubleshoot, rarely using it for informational messages that I find instead more helpfully place in the ULS logs for example.

    I am going to think about this more though…


  4. Cool, I had just assumed up to this point everyone was logging into the ULS logs, it hadn’t occurred to me that anywhere else was an option.

Leave a Reply

Your email address will not be published. Required fields are marked *