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:
C#:
-
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 Comments »
RSS feed for comments on this post. TrackBack URL





















Articles & Research
SharePoint Architecture
Personal/Off-Topic
Latest Free SharePoint Software
SecureCenter For SharePoint
SharePoint Security Assurance Program™
Free Online SharePoint Security Tools
Online SharePoint Security Health Assessment
Article Or Research Filed Under 
Presumably this is why we have had issues using Log4Net to consistently log to the system event logs too…
Comment by Jim — May 27, 2008 @ 1:28 pm
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.
Comment by Peter — May 27, 2008 @ 3:27 pm
[...] Remember to Elevate For Event Log Entries [...]
Pingback by Links (5/27/2008) « Steve Pietrek - Everything SharePoint — May 27, 2008 @ 5:52 pm
Jim:
Yup, I am pretty sure this is why we where having problems with it.
Peter:
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…
Comment by Adam Buenz — May 28, 2008 @ 7:29 am
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.
Comment by Peter — May 28, 2008 @ 10:00 am
Indeed this is an helpful post.Good Food for thought for why to write to Event Log.
Comment by Gaurav — June 14, 2008 @ 11:34 pm