4

I was following this tutorial:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdiagnosticsservicebase.aspx

But I got an access denied:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    using (new SPMonitoredScope("RenderMethod"))
    {
         writer.Write(DisplayMessage);

         DiagnosticsService myULS = DiagnosticsService.Local; // ACCESS DENIED
         if (myULS != null)
         {
             SPDiagnosticsCategory cat = myULS[CategoryId.Provisioning];
             string format = "Tracing test of {0} service";
             myULS.WriteTrace(1, cat, TraceSeverity.Verbose, format, myULS.TypeName);
             myULS.WriteEvent(1, cat, EventSeverity.Information, format, myULS.TypeName);
         }
    }
}

I tried the following on a console application and the diagSvc is always null

SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace( 0, // custom trace id
    new SPDiagnosticsCategory("My category", 
        TraceSeverity.Monitorable, 
        EventSeverity.Error), // create a category
    TraceSeverity.Monitorable, // set the logging level of this record
    "Writing to the ULS log:  {0}", // custom message
    new object[] { "SharePoint rocks!"} // parameters to message
);

4 Answers 4

5

The "registration" of a subclass of SPDiagnosticBase requires Farm admin privileges.

The registration occurs on first access.

You'll need to call "DiagnosticsService.Local" in a context with Farm admin priveleges.

When you ran your console app you probably ran it as a farm admin user and the registration succeeded.

If you try to install your code on another system it will most likely fail again.

I would recommend to call DiagnosticService.Local in the FeatureInstalled method of a FeatureEventReceiver as they are called with Farm admin priveleges.

0

If you're running on SP2010, you probably need SPSecurity.RunWithElevatedPrivileges. I realize this is a very old question, but fellow Googlers might find this helpful :)

-1

I found the answer, and posted on my blog here:

http://www.sharepoint2010blogger.com/2010/10/10/spfarm-gets-null-or-spdiagnosticsservice-local-gets-null/

-1

Open Project Properties -> Build Target -> Platform Target -> Change from x86 to x64. Save the project and execute again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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