Error when trying to use LogToOperations

Aug 5, 2010 at 9:12 PM

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Access denied.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[SecurityException: Access denied.]
   Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate() +27017467
   Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.Update() +803
   Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocalToFarm(SPFarm farm) +451
   Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocal() +86
   Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsService.get_Local() +50
   Microsoft.Practices.SharePoint.Common.Logging.EventLogLogger.Log(String message, Int32 eventId, EventSeverity severity, String category) +60
   Microsoft.Practices.SharePoint.Common.Logging.SharePointLogger.WriteToOperationsLog(String message, Int32 eventId, EventSeverity severity, String category) +134


Coordinator
Aug 5, 2010 at 10:33 PM

You must register your event sources before you log.  SharePoint uses the name of the area as the event source when logging to the event log.  This must be done on every WFE.  If you do not, then the Event log framework attempts to create the event source.  From a content web you will not have sufficient permissions to create the event source.  This information is detailed in the guide for the logger, and we provide a helper class to register all of the event sources for configured areas (DiagnosticsAreaEventSource).  I will be creating a blog entry within the next couple of days on how to write a timer job to accomplish this (just testing that now).


Aug 9, 2010 at 9:43 PM
Thanks for your reply. I did use the helper class to create the event source. I even checked the registry and it exists. But I still have the same error. Are there any permissions that may cause this. I do see in the trace that it updates at the farm level. - Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocalToFarm(SPFarm farm) +451
Aug 9, 2010 at 9:54 PM

Further investigation revealed that the code breaks at the following line in EventLogLogger.Log method

  DiagnosticsService diagnosticService = DiagnosticsService.Local;

 


Coordinator
Aug 9, 2010 at 9:58 PM

Looking at the callstack it does appear that its updating a persisted object as you indicated.  I'm pretty sure this is becuase the logger has never been used before, and it is attempting to automatically register the logger under the covers.  This behavior changed between beta and release.  At release it requires elevated permissions for SharePoint to initially register the logger.  The SharePoint SPDiagnosticsServiceBase will attempt to register the logger automatically for you if it hasn't been registered, and this is causing the security exception to be thrown.

There is a register method provided as part of DiagnosticsService.  Call register from a context where you have admin privleges, like a console app or a feature installed event.  You only need to this once, then its registered.

Looking at our documentation, we didn't make this very clear.  We'll look to put a note into the documentation to clear this up.  Please let me know if this doesn't work for you.

 

Coordinator
Aug 9, 2010 at 10:21 PM

Yes, I'm pretty sure this is the registration issue.  Please try the fix mentioned above.  We will be fixing our documentation on the topic.