This project is read-only.

Creating a Trace Message During Development

Typical Goals

In this scenario, you want to write debugging information into the ULS trace log.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll. Use the SharePoint service locator to get a reference to a Microsoft.Practices.SPG.Common.Logging.ILogger interface and invoke the TraceToDeveloper method. You can include descriptive strings, event IDs, exceptions, trace severity levels and category strings as arguments to the TraceToDeveloper method.

Quickstart

For an extended example of how to write debug trace information using the SharePoint logger, see [TBD].

Creating a Trace Message During Development

The following code demonstrates how to create a trace message.
C#
ILogger logger = SharePointServiceLocator.Current.GetInstance<ILogger>();
logger.TraceToDeveloper("Unexpected condition");

The TraceToDeveloper method writes to the ULS trace log.

Usage Notes

The TraceToDeveloper method has eleven overloaded versions that allow you to specify optional information such as trace severity level, event ID, exception data and category string. Here are some examples of using these overloads.
C#
// ILogger logger = ...
// Exception ex = ...
// string msg = ...
// string category = ...

// simple trace 
logger.TraceToDeveloper(msg);

// trace an event 
logger.TraceToDeveloper(msg, (int) EventLogEventId.SkuNotFound);

// trace with a trace severity 
logger.TraceToDeveloper(msg, TraceSeverity.High);

// trace with a category string
Logger.TraceToDeveloper(msg, category);

// trace with an event ID and a trace severity level
logger.TraceToDeveloper(msg, (int) EventLogEventId.PartnerNotFound,
                       EventLogEntryType.Error)

// trace with an event ID, a trace severity level and a category string 
logger.TraceToDeveloper(msg, (int) EventLogEventId.PartnerNotFound,
                       TraceSeverity.Unexpected, category)

// trace an exception 
logger.TraceToDeveloper(ex);

// trace an exception with an additional error message
logger.TraceToDeveloper(ex, msg);

// trace an exception with an additional error message and 
// an application-defined event id
logger.TraceToDeveloper(ex, msg, (int) EventLogEventId.SkuNotFound);

// trace an exception with an additional error message, a default event id,
// a trace severity level and a category string
logger.LogToOperations(ex, msg, 0, EventLogEntryType.Error, category)

// trace an exception with a default event id, trace severity string and
// a category string 
logger.TraceToDeveloper(ex, 0, TraceSeverity.Verbose, category) 


The EventLogEventId enumeration is user defined. It is a recommended practice to encode your application's event types as enumerated values.

The TraceSeverity enumeration is provided by SharePoint. It is found in the Microsoft.SharePoint.Administration namespace. For more information see The TraceSeverity Enumeration in MSDN.



To give feedback.
Copyright (c) 2007 by Microsoft Corporation. All rights reserved.

Last edited Jun 16, 2009 at 8:51 PM by ckeyser, version 1

Comments

No comments yet.