Logging is always a subject which everybody wants but not does.
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Administration;
namespace myCustomLogger.Logger
{
class myLogger : SPDiagnosticsServiceBase
{
public static string customDiagnosticAreaName = “I Like SharePoint”;
private static myLogger _Current;
public static myLogger Current
{
get
{
if (_Current == null)
{
_Current = new myLogger();
}
return _Current;
}
}
private myLogger()
: base(“I Like SharePoint”, SPFarm.Local)
{
}
protected override IEnumerable
{
List
{
new SPDiagnosticsArea(customDiagnosticAreaName, new List
{
new SPDiagnosticsCategory(“I Like SharePoint”, TraceSeverity.Unexpected, EventSeverity.Error),
new SPDiagnosticsCategory(“I Like SharePoint”, TraceSeverity.Monitorable, EventSeverity.Information)
})
};
return areas;
}
public static void LogUnexpectedError(string categoryName, string errorMessage)
{
SPDiagnosticsCategory category = myLogger.Current.Areas[customDiagnosticAreaName].Categories[categoryName];
myLogger.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
}
public static void LogInformation(string categoryName, string errorMessage)
{
SPDiagnosticsCategory category = myLogger.Current.Areas[customDiagnosticAreaName].Categories[categoryName];
myLogger.Current.WriteTrace(0, category, TraceSeverity.Monitorable, errorMessage);
}
public static void LogUnexpectedErrorEvent(string categoryName, string errorMessage)
{
SPDiagnosticsCategory category = myLogger.Current.Areas[customDiagnosticAreaName].Categories[categoryName];
myLogger.Current.WriteEvent(0, category, EventSeverity.Error, errorMessage, null);
}
public static void LogInformationEvent(string categoryName, string errorMessage)
{
SPDiagnosticsCategory category = myLogger.Current.Areas[customDiagnosticAreaName].Categories[categoryName];
myLogger.Current.WriteEvent(0, category, EventSeverity.Information, errorMessage, null);
}
}
}
[/sourcecode]
And then you can call it with those examples:
[sourcecode language=”csharp”]
myLogger.LogUnexpectedError(“I Like SharePoint Error”, “my Message”);
myLogger.LogUnexpectedErrorEvent(“I Like SharePoint Error”, “my Message”);
myLogger.LogInformation(“I Like SharePoint Info”, “my Message”);
[/sourcecode]
Leave a Reply