Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:
http://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".

share|improve this question
    
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
add comment

1 Answer

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

share|improve this answer
    
Are you referring to implementing IDiagnosticsLevel and IDiagnosticsManager? –  xdhmoore Apr 20 '12 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 –  C. Marius - MVP Apr 20 '12 at 17:50
    
Is this available in 2007? These all refer to 2010, and it appears some of this stuff is new to 2010. –  xdhmoore Apr 25 '12 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. –  xdhmoore Apr 27 '12 at 0:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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