I am trying to write entries into logging and have trouble doing so. Using Client object model on SharePoint foundation. Code is running from a console app.

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("CustomLog1", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected,
               ex.Message, ex.TargetSite.ToString());

I get the error "Object reference not set to an instance of an object" then tried this

SPDiagnosticsCategory category = new SPDiagnosticsCategory("CustomLog1", TraceSeverity.Verbose, EventSeverity.Verbose);
SPDiagnosticsService diagnosticService = SPDiagnosticsService.Local;
diagnosticService.WriteTrace(100, category, TraceSeverity.Medium,"CL tracing", "CL params");

Still same error.

  • 1
    is the SPTraceV4 service up & running? Apr 12 '12 at 15:12
  • I am new to SP. Can you tell me how to check it?
    – Yalaman
    Apr 12 '12 at 15:28
  • 1
    you can use some SP+PowerShell script: " Get-Service | Where-Object {$_.name -eq "SPTraceV4"} | fl " or check for services manually by invoking services.msc via the command prompt (in Windows). Apr 12 '12 at 16:36
  • posted some more info in response below. Apr 12 '12 at 16:46

Start with a check on the status of the 'SPTraceV4' service - this SP+PowerShell code snippet can help

Get-Service | Where-Object {$_.name -eq "SPTraceV4"} | fl 

or use services.msc inside the Windows ("DOS") command prompt.

Also, check the account being used for the SPTraceV4 Service as described in this post - Using a domain account for SPTraceV4 service.

UPDATE: This (working) code segment uses 'SPDiagnosticsService.Local.WriteTrace' to write to the ULS log file. The 'Local' property gets an object instance of the Diagnostics Service that is currently running. (use 'diagnosticService.Local.WriteTrace' in the last line of your code).

SPDiagnosticsCategory category = new SPDiagnosticsCategory("CustomLog1", TraceSeverity.Verbose, EventSeverity.Verbose);
SPDiagnosticsService.Local.WriteTrace(123, category, TraceSeverity.Verbose, "This is a Foo Test Message", null);

UPDATE2: This code snippet works too -

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("CustomLog1", TraceSeverity.Verbose, EventSeverity.Error), TraceSeverity.Verbose,"This is the second Foo Test Message", "CL params");

UPDATE3: My code segments above are for apps running on the server only - ULS logging is not for remote calls.

  • I have used the PS command and found that SPTraceV4 is running Name : SPTraceV4 DisplayName : SharePoint 2010 Tracing Status : Running DependentServices : {} ServicesDependedOn : {} CanPauseAndContinue : False CanShutdown : False CanStop : True ServiceType : Win32OwnProcess
    – Yalaman
    Apr 12 '12 at 17:29
  • I have tried with that local.writetrace for the very first time creating an object instance for spdiagnosticservice.local and even that is throwing the same old error.
    – Yalaman
    Apr 12 '12 at 18:44
  • 1
    I just noticed your (2nd) code snippet in that you obtain the local instance of the Diagnostic Service (SPDiagnosticsService.Local) so that should be fine. I have tried both your code segments (see above) with no significant changes and both appear to work. Perhaps, we should continue the diagnosis elsewhere? Apr 12 '12 at 18:56
  • 1
    could you post your exception message & showing the stack flow? (by putting your above code segment in a try-catch block). Could you say more about your SP application (presuming 64-bit app running on the server itself) ? Apr 12 '12 at 18:59
  • see UPDATE3 above. Apr 12 '12 at 19:09

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.