SPTraceProvider

This supplements the SPTraceProvider class. This adds a layer of abstraction to provide the following: Minimum severity logging - only record logs above defined minimum. This allows verbose/developer logs to be available during development, then simply switched off for production code, by changing a single variable. Log targetting - log to Trace logs OR the Event logs. Event logs are sometimes much easier to work with than Trace logs; however Trace logs require administrative permission at the server, which is often not available in a production environment. Additionally extensible to accomodate future log targets.
Language:
C#
Keywords:
Code Snippet

public static class Logging
{
    public enum LoggingLevels
    {
        Error = 10, // any Exception
        Basic = 30, // Issue CRUD, Issue Action CRUD
        Detail = 50, // SPList operations, SQL operations
        Verbose = 70, // Method Completion
        Debug = 100 // Call Stack like detail
    }
    public static SPTraceProvider.TraceSeverity ToSPTraceSeverity(this LoggingLevels Level)
     {
     SPTraceProvider.TraceSeverity sev = SPTraceProvider.TraceSeverity.Unassigned;
     switch (Level)
     {
        case LoggingLevels.Basic: sev = SPTraceProvider.TraceSeverity.Unassigned; break;
        case LoggingLevels.Detail: sev = SPTraceProvider.TraceSeverity.InformationEvent; break;
        case LoggingLevels.Verbose: sev = SPTraceProvider.TraceSeverity.Medium; break;
        case LoggingLevels.Debug: sev = SPTraceProvider.TraceSeverity.Verbose; break;
    }
     return sev;
}
public static System.Diagnostics.EventLogEntryType ToEventLogEntryType(this LoggingLevels Level)
{
    System.Diagnostics.EventLogEntryType EntryType = System.Diagnostics.EventLogEntryType.Information;
    switch (Level)
    {
     case LoggingLevels.Basic:
         EntryType = System.Diagnostics.EventLogEntryType.Information;
        break;
    case LoggingLevels.Detail:
        EntryType = System.Diagnostics.EventLogEntryType.Information;
        break;
    case LoggingLevels.Verbose:
        EntryType = System.Diagnostics.EventLogEntryType.Information;
        break;
    case LoggingLevels.Debug:
        EntryType = System.Diagnostics.EventLogEntryType.Information;
        break;
    }
    return EntryType;
}
const LoggingLevels MaxLogLevel = LoggingLevels.Basic;
public enum LogTargets
{
    None,
    EventLogs,
    TraceLogs
}
const LogTargets LogTarget = LogTargets.TraceLogs;
public static void WriteEntry(LoggingLevels Level, string Product, string msg)
{
    try
    {
        if (Level > MaxLogLevel)
            return;
        switch (LogTarget)
        {
            case LogTargets.None:
                break;
            case LogTargets.EventLogs:
                System.Diagnostics.EventLog.WriteEntry("Trax: " + Product + "." + msg, msg, Level.ToEventLogEntryType());
                break;
            case LogTargets.TraceLogs:
                SPTraceProvider.RegisterTraceProvider();
                SPTraceProvider.WriteTrace(0, Level.ToSPTraceSeverity(), Guid.Empty, "Trax", Product, string.Empty, msg);
                SPTraceProvider.UnregisterTraceProvider();
                break;
        }
    }
    catch (Exception)
    {
    }
}


Created 2012-01-25
comments powered by Disqus
Login