SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer


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.