I\'d like to log stuff in my Sharepoint Web Parts, but I want it to go into the ULS. Most examples that I\'ve found log into the Event Log or some other file, but I did not
The credit goes to: http://msdn.microsoft.com/en-us/library/gg512103(v=office.14).aspx
I have just published a post on my blog, but pasting the code here.
Define name of your solution in the code for following line:
private const string PRODUCT_NAME = "My Custom Solution";
Following are sample code on how to use it:
UlsLogging.LogInformation("This is information message");
UlsLogging.LogInformation("{0}This is information message","Information:");
UlsLogging.LogWarning("This is warning message");
UlsLogging.LogWarning("{0}This is warning message", "Warning:");
UlsLogging.LogError("This is error message");
UlsLogging.LogError("{0}This is error message","Error:");
Following is the code:
using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;
namespace MyLoggingApp
{
public class UlsLogging : SPDiagnosticsServiceBase
{
// Product name
private const string PRODUCT_NAME = "My Custom Solution";
#region private variables
// Current instance
private static UlsLogging _current;
// area
private static SPDiagnosticsArea _area;
// category
private static SPDiagnosticsCategory _catError;
private static SPDiagnosticsCategory _catWarning;
private static SPDiagnosticsCategory _catLogging;
#endregion
private static class CategoryName
{
public const string Error = "Error";
public const string Warning = "Warning";
public const string Logging = "Logging";
}
private static UlsLogging Current
{
get
{
if (_current == null)
{
_current = new UlsLogging();
}
return _current;
}
}
// Get Area
private static SPDiagnosticsArea Area
{
get
{
if (_area == null)
{
_area = UlsLogging.Current.Areas[PRODUCT_NAME];
}
return _area;
}
}
// Get error category
private static SPDiagnosticsCategory CategoryError
{
get
{
if (_catError == null)
{
_catError = Area.Categories[CategoryName.Error];
}
return _catError;
}
}
// Get warning category
private static SPDiagnosticsCategory CategoryWarning
{
get
{
if (_catWarning == null)
{
_catWarning = Area.Categories[CategoryName.Warning];
}
return _catWarning;
}
}
// Get logging category
private static SPDiagnosticsCategory CategoryLogging
{
get
{
if (_catLogging == null)
{
_catLogging = Area.Categories[CategoryName.Logging];
}
return _catLogging;
}
}
private UlsLogging()
: base(PRODUCT_NAME, SPFarm.Local)
{
}
protected override IEnumerable ProvideAreas()
{
var cat = new List{
new SPDiagnosticsCategory(CategoryName.Error, TraceSeverity.High,EventSeverity.Error),
new SPDiagnosticsCategory(CategoryName.Warning, TraceSeverity.Medium,EventSeverity.Warning),
new SPDiagnosticsCategory(CategoryName.Logging,TraceSeverity.Verbose,EventSeverity.Information)
};
var areas = new List();
areas.Add(new SPDiagnosticsArea(PRODUCT_NAME, cat));
return areas;
}
// Log Error
public static void LogError(string msg)
{
UlsLogging.Current.WriteTrace(0, CategoryError, TraceSeverity.High, msg);
}
public static void LogError(string msg,params object[] args)
{
UlsLogging.Current.WriteTrace(0, CategoryError, TraceSeverity.High, msg,args);
}
// Log Warning
public static void LogWarning(string msg)
{
UlsLogging.Current.WriteTrace(0, CategoryWarning, TraceSeverity.Medium, msg);
}
public static void LogWarning(string msg, params object[] args)
{
UlsLogging.Current.WriteTrace(0, CategoryWarning, TraceSeverity.Medium, msg,args);
}
// Log Information
public static void LogInformation(string msg)
{
UlsLogging.Current.WriteTrace(0, CategoryLogging, TraceSeverity.Verbose, msg);
}
public static void LogInformation(string msg,params object[] args)
{
UlsLogging.Current.WriteTrace(0, CategoryLogging, TraceSeverity.Verbose, msg,args);
}
}
}