The Sharepoint Trace Log Sample given at http://msdn.microsoft.com/en-us/library/aa979522.aspx & explained at http://msdn.microsoft.com/en-us/library/aa979595.aspx does not work in in Sharepoint 2010. Any ideas of how to Write to Trace Log from a webpart (non-sandboxed)?

This is dupe of question: https://stackoverflow.com/questions/2295333/trace-log-example-does-not-work-in-sharepoint-2010 but i'm asking it on this community to see if anyone has an answer.


This has really improved in SP2010. Now you can write to the ULS logs easily even with SharePoint Foundation. All you have to do is this:

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

Works great, but not available in sandboxed solutions...

  • Hmm this raises another question: how would you go about logging in a sandboxed wp? – Anders Rask Mar 26 '10 at 7:58
  • 1
    You have three options (as I see it) - custom user code proxy - log to a list - log to a database using an external list – Wictor Wilen MCA MCM MVP Mar 26 '10 at 8:04

Just to elaborate on Wictor's answer, his example will log 'Unknown' to the Area column. SPDiagnosticsCategory has an 'Area' property, but unfortunately its protection level is set to internal.

However, you can set it using some reflection code. Listed below is an example of how to write to the SharePoint 2010 trace log using reflection. I use it in a generic library that allows logging to both the SharePoint 2007 and 2010 trace logs. In that case reflection must be used as the library is compiled against the 2007 version of Microsoft.SharePoint.dll.

/// <summary>
/// Write to the SharePoint 2010 trace log
/// </summary>
/// <param name="tag">The Tag to write to the log.</param>
/// <param name="level">The Trace Severity Level to write to the log.</param>
/// <param name="area">The name of the product to write to the log.</param>
/// <param name="categoryName">The name of the category to write to the log.</param>
/// <param name="message">The actual message to write</param>
public static void WriteTraceSP2010(uint tag, TraceSeverity level, string area, string categoryName, string message)
    string traceSeverityName = null;
    string eventSeverityName = "None";

    // ** Map the SP2007 TraceSeverity Enum to the SP2010 ones
    switch (level)
        case TraceSeverity.Assert:
        case TraceSeverity.CriticalEvent:
        case TraceSeverity.Exception:
        case TraceSeverity.Unexpected:
        case TraceSeverity.WarningEvent:
            traceSeverityName = "Unexpected";
        case TraceSeverity.InformationEvent:
        case TraceSeverity.Unassigned:
            traceSeverityName = "Medium";
            // ** Anything else is mapped 1:1
            traceSeverityName = level.ToString();

    // ** Load the SharePoint 2010 version of Microsoft.SharePoint
    Assembly assembly = Assembly.Load("Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c");

    // ** Get SPDiagnosticsService.Local
    Type spDiagnosticsServiceType = assembly.GetType("Microsoft.SharePoint.Administration.SPDiagnosticsService");
    object service = spDiagnosticsServiceType.GetProperty("Local", BindingFlags.Static | BindingFlags.Public).GetValue(
        Activator.CreateInstance(spDiagnosticsServiceType), null);

    // ** Get the correct enum value
    Type eventSeverityType = assembly.GetType("Microsoft.SharePoint.Administration.EventSeverity");
    object eventSeverity = eventSeverityType.GetField(eventSeverityName).GetValue(null);
    Type traceSeverityType = assembly.GetType("Microsoft.SharePoint.Administration.TraceSeverity");
    object traceSeverity = traceSeverityType.GetField(traceSeverityName).GetValue(null); ;

    // ** Create a new DiagnosticsCategory
    Type diagnosticsCategoryType = assembly.GetType("Microsoft.SharePoint.Administration.SPDiagnosticsCategory");
    Object diagnosticsCategory = Activator.CreateInstance(diagnosticsCategoryType, new Object[] { categoryName, 
        traceSeverity, eventSeverity});

    // ** Set the area
    Type diagnosticsAreaType = assembly.GetType("Microsoft.SharePoint.Administration.SPDiagnosticsArea");
    Object diagnosticsArea = Activator.CreateInstance(diagnosticsAreaType, new Object[] { area, null });
    diagnosticsCategoryType.GetProperty("Area").SetValue(diagnosticsCategory, diagnosticsArea, null);

    // ** Execute the WriteTrace method
    spDiagnosticsServiceType.GetMethod("WriteTrace").Invoke(service, new object[] { tag, diagnosticsCategory,
        traceSeverity, message, null });

Note that the TraceSeverity enumeration is the one used in MSDN's SharePoint 2007 ULS sample code.

More information can be found on the MSDN web site.

  • Great addition! – Wictor Wilen MCA MCM MVP Apr 27 '10 at 19:38
  • Thanks. I had to write this using reflection (Painful!) to make it work in both SP2007 and SP2010 so I noticed this ability. – Jeroen Ritmeijer - Muhimbi Apr 28 '10 at 5:41
  • Area property of category is read only. Your code example is invalid. – Muhammad Hasan Khan Apr 30 '10 at 9:03
  • Hi Hasan. I'll look into it, but it works fine for me. Maybe because my implementation uses reflection to set the property. – Jeroen Ritmeijer - Muhimbi May 2 '10 at 11:56
  • Now added a version of the code that uses reflection to set the Area property. – Jeroen Ritmeijer - Muhimbi May 3 '10 at 11:54

Microsoft provides excellent guidance for SharePoint 2010 logging through their Patterns and Practices group: http://msdn.microsoft.com/en-us/library/ff770300.aspx

The guide explains sandboxed versus non-sandboxed loggging.

I've used the code provided on codeplex from this group to log to ULS, the Windows Event Log and to trace messages that can be seen by WinDebug if the other two fail.

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.