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".

  • Do you have some code that might show exactly what you are doing? – Dave Wise Apr 19 '12 at 15:29
  • You may need to inherit from the SharePoint WebPart class rather than the System.Web.UI.WebControls.WebParts.WebPart class. – John Chapman Apr 19 '12 at 21:20
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

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.