I was following this tutorial:


But I got an access denied:

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

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


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.


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 :)


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



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, privacy policy and cookie policy

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