Condividi tramite


SPDiagnosticsServiceBase.WriteTrace Method

Writes a trace to the Microsoft SharePoint Foundation trace log.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
Public Sub WriteTrace ( _
    id As UInteger, _
    category As SPDiagnosticsCategory, _
    severity As TraceSeverity, _
    output As String, _
    ParamArray data As Object() _
)
'Usage
Dim instance As SPDiagnosticsServiceBase
Dim id As UInteger
Dim category As SPDiagnosticsCategory
Dim severity As TraceSeverity
Dim output As String
Dim data As Object()

instance.WriteTrace(id, category, severity, _
    output, data)
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.

  • 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.

Remarks

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.

Examples

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();
        }
    }
}

See Also

Reference

SPDiagnosticsServiceBase Class

SPDiagnosticsServiceBase Members

Microsoft.SharePoint.Administration Namespace

Areas