次の方法で共有


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

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

  • Anonymous
    September 01, 2010
    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

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

  • Anonymous
    October 01, 2010
    The comment has been removed

  • Anonymous
    October 10, 2010
    Alex, please check my blog here: It might help you. www.sharepoint2010blogger.com/.../spfarm-gets-null-or-spdiagnosticsservice-local-gets-null

  • Anonymous
    November 03, 2011
    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

  • Anonymous
    November 03, 2011
    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

  • Anonymous
    December 19, 2011
    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!

  • Anonymous
    February 28, 2012
    The comment has been removed

  • Anonymous
    April 08, 2012
    Nothing is happening for me as well after running this code..plz help

  • Anonymous
    February 21, 2014
    On Powershel: Microsoft.SharePoint.Administration.SPDiagnosticsService. Not with a "s" at the end. Gave me a hard time...

  • Anonymous
    April 26, 2015
    The above code will throw security exception unless executed under SPSecurity.RunWithElevatedPrivileges() method.

  • Anonymous
    September 22, 2015
    H i do we need to do any other manual settings.. Like adding user to performance user group ?