3

With Sharepoint 2007 I am using RunWithElevatedPrivileges to write to the Event Log, but if I place the call inside the Webpart constructor, I get a "Request Failed" message. However, if I put this in the CreateChildControls method, it works fine. Within the RunWithElevatedPrivileges delegate, I am simply creating a new EventLog() object, nothing more.

I have seen others with the same issue. For example:
https://stackoverflow.com/questions/2387433/trust-set-to-full-but-web-part-still-causes-securityexception
Does anyone know why this is?

[Update:]

This does not work:

 public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart { private ILogger logger; public SSOLogger() { EventLog log = new EventLog(); } protected override void CreateChildControls() { } } 

I am getting the following System.Security.SecurityException:

{"Request for the permission of type 'System.Diagnostics.EventLogPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=...' failed."}

The following also does not work:

 public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart { private ILogger logger; public SSOLogger() { EventLog log; SPSecurity.RunWithElevatedPrivileges(delegate() { log = new EventLog(); }); } protected override void CreateChildControls() { } } 

This gives me just a

Failed Request.

message in the SecurityException.

The following, however, works:

 public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart { private ILogger logger; public SSOLogger() { } protected override void CreateChildControls() { EventLog log = new EventLog(); } } 

I know writing to the event log requires elevated permissions for non-admin accounts, but this does not even work when adding the webpart from a "System Account".

2
  • Do you have some code that might show exactly what you are doing? Commented Apr 19, 2012 at 15:29
  • You may need to inherit from the SharePoint WebPart class rather than the System.Web.UI.WebControls.WebParts.WebPart class. Commented Apr 19, 2012 at 21:20

1 Answer 1

0

Actually I would suggest you use the SPDiagnostics namespace functions as a more sustainable solution. Writing to EventLog is not the best way to log stuff in SharePoint.

Besides why do it in Constructor? Do it in Init or Load but let the constructor do its work as it is not best practice to do a lot of stuff in constructor.

If you still work with EventLog you need to make sure the account you are impersonating has the permissions to write to the even log - e.g Farm Account or Web Application Pool.

C:\Marius

4
  • Are you referring to implementing IDiagnosticsLevel and IDiagnosticsManager? Commented Apr 20, 2012 at 16:27
  • Write to ULS (Universal Logging System) in SharePoint, see here blog.mastykarz.nl/logging-uls-sharepoint-2010 or here learningsharepoint.com/2010/07/04/… For more details see here for options: msdn.microsoft.com/en-us/library/gg512103.aspx Commented Apr 20, 2012 at 17:50
  • Is this available in 2007? These all refer to 2010, and it appears some of this stuff is new to 2010. Commented Apr 25, 2012 at 22:52
  • I don't need to do it in the constructor. I was just curious as to why it seems that the permissions are different. Commented Apr 27, 2012 at 0:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.