How to write custom logs into ULS logs – SharePoint Foundation 2010 ( WSS 4.0) ?

How to write custom logs into ULS logs – SharePoint Foundation 2010 ( WSS 4.0) ?

Rate This
  • Comments 9

Here is a sample script for writing your custom entries into ULS log. In WSS 3.0 it was little difficult as there was no any direct APIs to do it like in MOSS. But in 2010 version, SharePoint Foundation (WSS 4.0) also has this facility.

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
 
namespace WriteIntoULSLog
{
    public class Program
    {
        public static void Main(string[] args)
        {
             
            SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
            diagSvc.WriteTrace( 0, 
                                                    new SPDiagnosticsCategory("Sowmyan's category",  TraceSeverity.Monitorable, EventSeverity.Error), 
                                                    TraceSeverity.Monitorable,
                                                    "Writing to the ULS log:  {0}",
                                                    new object[] { "SharePoint 2010 rocks!"});           
            
  
        }
    }
}

PowerShell

$diagSvc = [Microsoft.SharePoint.Administration.SPDiagnosticsServices]::Local
$category = new-object Microsoft.SharePoint.Administration.SPDiagnosticsCategory(“My Category”,
                                   [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable,
                                  [Microsoft.SharePoint.Administration.EventSeverity]::Error )
$diagSvc.WriteTrace(0, $category, [Microsoft.SharePoint.Administration.TraceSeverity]::Monitorable, “Write your log here” )
Comments
  • Thanks for the tip! I get a security exception, though:

    Security Exception

    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

    Exception Details: System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.

    System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly) +563

      System.Diagnostics.EventLog.SourceExists(String source, String machineName) +264

      System.Diagnostics.EventLog.VerifyAndCreateSource(String sourceName, String currentMachineName) +84

      System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +377

      System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +112

      System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category) +28

      Microsoft.SharePoint.Administration.<>c__DisplayClass2.<WriteEvent>b__0() +83

      Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27492054

      Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.WriteEvent(UInt16 id, SPDiagnosticsCategory category, EventSeverity severity, String output, Object[] data) +517

  • Ah! It would have helped had I used WriteTrace instead of WriteEvent... Works great, Thx!

  • So for some reason when I execute this code (pretty much exactly what you have) on the web app it does not write anything - I'm a little confused as to why nothing's getting logged! anyone else have this issue?

    SPDiagnosticsService diagSvc = SPDiagnosticsService.Local; // so far so good...

    diagSvc.WriteTrace( 0, new SPDiagnosticsCategory("Hello ULS",  TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable, "Writing to the ULS logs ain't happening.", null );

    // the line above does not throw any error - it just just executes without ever writing anything!! same line is placed in a console app, and it works like a charm.

    Note that I am writing this inside say a web part or other typical code areas where you'd want to log activity in a custom SP solution... any thoughts?

  • Alex, please check my blog here: It might help you.

    www.sharepoint2010blogger.com/.../spfarm-gets-null-or-spdiagnosticsservice-local-gets-null

  • An excellent, unit-test-friendly way to log to the ULS is by using the SharePoint Guidance Library:

    http://spg.codeplex.com/

    It includes Full-Trust proxies to facilitate logging from Sandbox solutions.

    Chris

  • An excellent, unit-test-friendly way to log to the ULS is by using the SharePoint Guidance Library:

    http://spg.codeplex.com/

    It includes Full-Trust proxies to facilitate logging from Sandbox solutions.

    Chris

  • This will just only log the custom string into defined area.When you need to customise areas then http://spg.codeplex.com is the only one way.

    Just follow there!

  • One remark here. Sharepoint doesnot rocks and to be honest sharepoint is big piece of crap.

  • Nothing is happening for me as well after running this code..plz help

Page 1 of 1 (9 items)
Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post