Classe per la scrittura nei log di SharePoint 2010
In questo esempio C# si può vedere come scrivere nei log di SharePoint 2010:
per usarle
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
namespace Sgart
{
public class LoggingService : SPDiagnosticsService
{
public const string INFO = "Sgart Info";
public const string WARNING = "Sgart Warning";
public const string ERROR = "Sgart Error";
public const string PRODUCTNAME = "Sgart sample";
public LoggingService()
: base("Sgart.it", SPFarm.Local)
{
}
protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
{
List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>{
new SPDiagnosticsArea(PRODUCTNAME, new List<SPDiagnosticsCategory>{
new SPDiagnosticsCategory(INFO, TraceSeverity.Verbose, EventSeverity.Information)
, new SPDiagnosticsCategory(WARNING, TraceSeverity.Monitorable, EventSeverity.Information)
, new SPDiagnosticsCategory(ERROR, TraceSeverity.Unexpected, EventSeverity.ErrorCritical)
})
};
return areas;
}
private static LoggingService _current;
public static LoggingService Current
{
get
{
if (_current == null)
_current = new LoggingService();
return _current;
}
}
public static void WriteInfo(string message)
{
SPDiagnosticsCategory category = Current.Areas[PRODUCTNAME].Categories[INFO];
Current.WriteTrace(0, category, TraceSeverity.Verbose, message);
}
public static void WriteWarning(string message)
{
SPDiagnosticsCategory category = Current.Areas[PRODUCTNAME].Categories[WARNING];
Current.WriteTrace(0, category, TraceSeverity.Medium, message);
}
public static void WriteWarning(Exception ex)
{
WriteError(ex.ToString());
}
public static void WriteError(string message)
{
SPDiagnosticsCategory category = Current.Areas[PRODUCTNAME].Categories[ERROR];
Current.WriteTrace(0, category, TraceSeverity.Unexpected, message);
}
}
}
C#
LoggingService.WriteInfo("messaggio");
LoggingService.WriteWarning("messaggio");
LoggingService.WriteError("messaggio");
LoggingService.WriteError(exception);