Advertisement
private775

C#: Custom SharePoint Logging Service

Feb 5th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint.Administration;
  6. using System.Diagnostics;
  7. using System.Runtime.CompilerServices;
  8. using System.IO;
  9.  
  10. namespace nova.tmre.tsm.premiumpayment.Common
  11. {
  12.     public class CustomSpLoggingService : SPDiagnosticsServiceBase
  13.     {
  14.  
  15.         public static string NovaDiagnosticAreaName = "SharePoint Custom Code";
  16.         public static readonly CustomSpLoggingService Current = new CustomSpLoggingService();
  17.         public static string eventLogName = "Application";
  18.  
  19.         private CustomSpLoggingService()
  20.             : base("Custom SharePoint Logging Service", SPFarm.Local)
  21.         {
  22.             try
  23.             {
  24.                 if (!EventLog.SourceExists(NovaDiagnosticAreaName))
  25.                     EventLog.CreateEventSource(NovaDiagnosticAreaName, eventLogName);
  26.             }
  27.             catch
  28.             {
  29.                 // do nothing
  30.             }
  31.         }
  32.  
  33.         public static class DiagnosticCategory
  34.         {
  35.             public const string WebPart = "WebPart";
  36.             public const string TimerJob = "TimerJob";
  37.             public const string FeatureActivation = "FeatureActivation";
  38.             public const string FeatureDeactivation = "FeatureDeactivation";
  39.         }
  40.  
  41.         protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
  42.         {
  43.             List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
  44.                 {
  45.                     new SPDiagnosticsArea(NovaDiagnosticAreaName, new List<SPDiagnosticsCategory>
  46.                     {
  47.                         new SPDiagnosticsCategory(DiagnosticCategory.WebPart, TraceSeverity.Unexpected, EventSeverity.Error),
  48.                         new SPDiagnosticsCategory(DiagnosticCategory.TimerJob, TraceSeverity.Unexpected, EventSeverity.Error),
  49.                         new SPDiagnosticsCategory(DiagnosticCategory.FeatureActivation, TraceSeverity.Unexpected, EventSeverity.Error),
  50.                         new SPDiagnosticsCategory(DiagnosticCategory.FeatureDeactivation, TraceSeverity.Unexpected, EventSeverity.Error)
  51.                     })
  52.                 };
  53.  
  54.             return areas;
  55.         }
  56.  
  57.  
  58.         public void LogDebug(string categoryName, string errorMessage)
  59.         {
  60. #if DEBUG
  61.             SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
  62.             this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
  63.             string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
  64.             System.Diagnostics.Trace.WriteLine(errorMessage, catString);
  65. #endif
  66.         }
  67.  
  68.  
  69.         public void LogMessage(string categoryName, string errorMessage)
  70.         {
  71.             SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
  72.             this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
  73.             string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
  74.             System.Diagnostics.Trace.WriteLine(errorMessage, catString);
  75.         }
  76.  
  77.         public void LogError(string categoryName, string errorMessage)
  78.         {
  79.             try
  80.             {
  81.                 SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
  82.                 this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
  83.                 string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
  84.                 System.Diagnostics.Trace.WriteLine(errorMessage, catString);
  85.                 if (EventLog.SourceExists(NovaDiagnosticAreaName))
  86.                 {
  87.                     EventLog.WriteEntry(NovaDiagnosticAreaName, errorMessage, EventLogEntryType.Error);
  88.                 }
  89.             }
  90.             catch { }
  91.  
  92.         }
  93.  
  94.         public void LogException(string categoryName, Exception ex)
  95.         {
  96.             try
  97.             {
  98.  
  99.  
  100.                 string msg = string.Empty;
  101.                 if (ex.TargetSite != null)
  102.                 {
  103.                     msg = string.Format("{0}.{1}:{2}", ex.TargetSite.DeclaringType.Namespace, ex.TargetSite.DeclaringType.Name, ex.TargetSite.Name);
  104.                 }
  105.  
  106.                 #region Log to Sharepoint log
  107.                 SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
  108.                 this.WriteTrace(0, category, TraceSeverity.Unexpected, string.Format("{0}: exception: {1}", msg, ex.GetType().Name));
  109.                 if (!string.IsNullOrEmpty(ex.Message)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.Message);
  110.                 if (!string.IsNullOrEmpty(ex.Source)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.Source);
  111.                 if (!string.IsNullOrEmpty(ex.StackTrace)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.StackTrace);
  112.                 if (ex.InnerException != null)
  113.                 {
  114.                     this.WriteTrace(0, category, TraceSeverity.Unexpected, string.Format("Inner Exception: {0}", ex.InnerException.GetType().Name));
  115.                     if (!string.IsNullOrEmpty(ex.InnerException.Message)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.Message);
  116.                     if (!string.IsNullOrEmpty(ex.InnerException.Source)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.Source);
  117.                     if (!string.IsNullOrEmpty(ex.InnerException.StackTrace)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.StackTrace);
  118.                 }
  119.                 #endregion
  120.  
  121.                 #region Log to system debug
  122.                 string catString = string.Format("{0}:{1}:{2}", NovaDiagnosticAreaName, categoryName, msg);
  123.                 System.Diagnostics.Trace.WriteLine(string.Format("{0}: exception: {1}", msg, ex.GetType().Name), catString);
  124.                 if (!string.IsNullOrEmpty(ex.Message)) System.Diagnostics.Trace.WriteLine(ex.Message, catString);
  125.                 if (!string.IsNullOrEmpty(ex.Source)) System.Diagnostics.Trace.WriteLine(ex.Source, catString);
  126.                 if (!string.IsNullOrEmpty(ex.StackTrace)) System.Diagnostics.Trace.WriteLine(ex.StackTrace, catString);
  127.                 if (ex.InnerException != null)
  128.                 {
  129.                     System.Diagnostics.Trace.WriteLine(string.Format("Inner Exception: {0}", ex.InnerException.GetType().Name), catString);
  130.                     if (!string.IsNullOrEmpty(ex.InnerException.Message)) System.Diagnostics.Trace.WriteLine(ex.InnerException.Message, catString);
  131.                     if (!string.IsNullOrEmpty(ex.InnerException.Source)) System.Diagnostics.Trace.WriteLine(ex.InnerException.Source, catString);
  132.                     if (!string.IsNullOrEmpty(ex.InnerException.StackTrace)) System.Diagnostics.Trace.WriteLine(ex.InnerException.StackTrace, catString);
  133.                 }
  134.                 #endregion
  135.  
  136.                 #region Log to Event log
  137.                 if (EventLog.SourceExists(NovaDiagnosticAreaName))
  138.                 {
  139.                     StringWriter sw = new StringWriter();
  140.                     sw.WriteLine(string.Format("{0}: exception: {1}", msg, ex.GetType().Name));
  141.                     if (!string.IsNullOrEmpty(ex.Message)) sw.WriteLine(ex.Message);
  142.                     if (!string.IsNullOrEmpty(ex.Source)) sw.WriteLine(ex.Source);
  143.                     if (!string.IsNullOrEmpty(ex.StackTrace)) sw.WriteLine(ex.StackTrace);
  144.                     EventLog.WriteEntry(NovaDiagnosticAreaName, sw.ToString());
  145.                 }
  146.                 if (ex.InnerException != null)
  147.                 {
  148.                     var exc = ex.InnerException;
  149.                     StringWriter sw = new StringWriter();
  150.                     sw.WriteLine(string.Format("{0}: exception: {1}", msg, exc.GetType().Name));
  151.                     if (!string.IsNullOrEmpty(ex.Message)) sw.WriteLine(exc.Message);
  152.                     if (!string.IsNullOrEmpty(ex.Source)) sw.WriteLine(exc.Source);
  153.                     if (!string.IsNullOrEmpty(ex.StackTrace)) sw.WriteLine(exc.StackTrace);
  154.                     EventLog.WriteEntry(NovaDiagnosticAreaName, sw.ToString());
  155.                 }
  156.                 #endregion
  157.             }
  158.             catch { }
  159.         }
  160.  
  161.         #region Utils
  162.         [MethodImpl(MethodImplOptions.NoInlining)]
  163.         public string GetCurrentMethod()
  164.         {
  165.             StackTrace st = new StackTrace();
  166.             StackFrame sf = st.GetFrame(1);
  167.  
  168.             return sf.GetMethod().Name;
  169.         }
  170.  
  171.         [MethodImpl(MethodImplOptions.NoInlining)]
  172.         public string GetCurrentClass()
  173.         {
  174.             StackTrace st = new StackTrace();
  175.             StackFrame sf = st.GetFrame(1);
  176.  
  177.             return sf.GetMethod().DeclaringType.Name;
  178.         }
  179.  
  180.         #endregion
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement