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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.