Export (0) Print
Expand All

SPDiagnosticsServiceBase.WriteTrace method

Writes a trace to the Microsoft SharePoint Foundation trace log.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public void WriteTrace(
	uint id,
	SPDiagnosticsCategory category,
	TraceSeverity severity,
	string output,
	params Object[] data
)

Parameters

id
Type: System.UInt32

The application-defined identifier for the trace.

category
Type: Microsoft.SharePoint.Administration.SPDiagnosticsCategory

The category of the trace.

severity
Type: Microsoft.SharePoint.Administration.TraceSeverity

The severity of the trace.

output
Type: System.String

The message. Optionally, the message may contain format placeholders so that the string can be passed to System.String.Format(string, Object[]) for formatting.

data
Type: []

The optional items to be replaced into the message format string.

You must use a category that is recognized by the service. For an example showing how to query a diagnostics services for areas and categories, see the Areas property.

Keep in mind that if you pass a value in the severity parameter that is less than the currently configured value for the category's TraceSeverity property, the trace is not written to the log.

The following example is a console application that writes a test message in the trace log.

using System;
using Microsoft.SharePoint.Administration;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
            SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];

            string format = "Test trace logging for category {0} in area {1}";
            diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, format, cat.Name, cat.Area.Name);

            Console.WriteLine(format, cat.Name, cat.Area.Name);
            Console.ReadLine();
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft