Logging in SharePoint

Logging in SharePoint
2 votes, 5.00 avg. rating (96% score)

Introduction

This post covers how to log information when working with SharePoint. We may require to log exception details or to log any data for information purpose
We will see how easy in SharePoint to log information using SharePoint Logger and how easy to view the logged information.

Skill Level – Medium

Logging in SharePoint

You have to add a reference to the Microsoft.Practices.SharePoint.Common.dll assembly, the Microsoft.Practices.ServiceLocation.dll assembly, and the Microsoft.SharePoint.dll assembly in order to work for logging

Following is the sample code which I have used

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.SharePoint;
namespace TestConsoleApplication
{
class Program
{
	public static void Main(string[] args)
	{
		ILogger logger = SharePointServiceLocator.Current.GetInstance<ILogger>();
		logger.TraceToDeveloper(new Exception("AdiEx2"), "Adi2", 9999, Microsoft.SharePoint.Administration.TraceSeverity.Unexpected, null);
	}
}
}

 

Following is the image how we can check the logs using ULS viewer.


Logging in SharePoint

Logging in SharePoint

More ways

	// ILogger logger = ...
	// Exception ex = ...
	// string msg = ...
	// string category = ...

	// Here is a 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,
						   TraceSeverity.Verbose);

	// 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.TraceToDeveloper(ex, msg, 0, TraceSeverity.High, category);

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

Conclusion

I generally rely on ULS viewer and filter logs based on event ID. This is a quick way I will figure out what is information that is logged relevant to us.Hope this adds value for developing better application and helpful for debugging or to fix bugs.

October 31, 2013 В· Adi В· No Comments
Tags: , , ,  В· Posted in: C#, Sharepoint 2010, Sharepoint Deployment

Leave a Reply

What is 3 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)