SharePoint create a custom Logger for your application

Logging is always a subject which everybody wants but not does.



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<spdiagnosticsarea> ProvideAreas()
{
List</spdiagnosticsarea><spdiagnosticsarea> areas = new List</spdiagnosticsarea><spdiagnosticsarea>
{
new SPDiagnosticsArea(customDiagnosticAreaName, new List<spdiagnosticscategory>
{
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);
}
}
}


And then you can call it with those examples:



myLogger.LogUnexpectedError("I Like SharePoint Error", "my Message");
myLogger.LogUnexpectedErrorEvent("I Like SharePoint Error", "my Message");
myLogger.LogInformation("I Like SharePoint Info", "my Message");


The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.



About Karsten Schneider 306 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.


*