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

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

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.

share|improve this answer
add comment

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
add comment

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

share|improve this answer
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.