Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint.Administration;
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- using System.IO;
- namespace nova.tmre.tsm.premiumpayment.Common
- {
- public class CustomSpLoggingService : SPDiagnosticsServiceBase
- {
- public static string NovaDiagnosticAreaName = "SharePoint Custom Code";
- public static readonly CustomSpLoggingService Current = new CustomSpLoggingService();
- public static string eventLogName = "Application";
- private CustomSpLoggingService()
- : base("Custom SharePoint Logging Service", SPFarm.Local)
- {
- try
- {
- if (!EventLog.SourceExists(NovaDiagnosticAreaName))
- EventLog.CreateEventSource(NovaDiagnosticAreaName, eventLogName);
- }
- catch
- {
- // do nothing
- }
- }
- public static class DiagnosticCategory
- {
- public const string WebPart = "WebPart";
- public const string TimerJob = "TimerJob";
- public const string FeatureActivation = "FeatureActivation";
- public const string FeatureDeactivation = "FeatureDeactivation";
- }
- protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
- {
- List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
- {
- new SPDiagnosticsArea(NovaDiagnosticAreaName, new List<SPDiagnosticsCategory>
- {
- new SPDiagnosticsCategory(DiagnosticCategory.WebPart, TraceSeverity.Unexpected, EventSeverity.Error),
- new SPDiagnosticsCategory(DiagnosticCategory.TimerJob, TraceSeverity.Unexpected, EventSeverity.Error),
- new SPDiagnosticsCategory(DiagnosticCategory.FeatureActivation, TraceSeverity.Unexpected, EventSeverity.Error),
- new SPDiagnosticsCategory(DiagnosticCategory.FeatureDeactivation, TraceSeverity.Unexpected, EventSeverity.Error)
- })
- };
- return areas;
- }
- public void LogDebug(string categoryName, string errorMessage)
- {
- #if DEBUG
- SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
- this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
- string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
- System.Diagnostics.Trace.WriteLine(errorMessage, catString);
- #endif
- }
- public void LogMessage(string categoryName, string errorMessage)
- {
- SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
- this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
- string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
- System.Diagnostics.Trace.WriteLine(errorMessage, catString);
- }
- public void LogError(string categoryName, string errorMessage)
- {
- try
- {
- SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
- this.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
- string catString = string.Format("{0}:{1}", NovaDiagnosticAreaName, categoryName);
- System.Diagnostics.Trace.WriteLine(errorMessage, catString);
- if (EventLog.SourceExists(NovaDiagnosticAreaName))
- {
- EventLog.WriteEntry(NovaDiagnosticAreaName, errorMessage, EventLogEntryType.Error);
- }
- }
- catch { }
- }
- public void LogException(string categoryName, Exception ex)
- {
- try
- {
- string msg = string.Empty;
- if (ex.TargetSite != null)
- {
- msg = string.Format("{0}.{1}:{2}", ex.TargetSite.DeclaringType.Namespace, ex.TargetSite.DeclaringType.Name, ex.TargetSite.Name);
- }
- #region Log to Sharepoint log
- SPDiagnosticsCategory category = CustomSpLoggingService.Current.Areas[NovaDiagnosticAreaName].Categories[categoryName];
- this.WriteTrace(0, category, TraceSeverity.Unexpected, string.Format("{0}: exception: {1}", msg, ex.GetType().Name));
- if (!string.IsNullOrEmpty(ex.Message)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.Message);
- if (!string.IsNullOrEmpty(ex.Source)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.Source);
- if (!string.IsNullOrEmpty(ex.StackTrace)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.StackTrace);
- if (ex.InnerException != null)
- {
- this.WriteTrace(0, category, TraceSeverity.Unexpected, string.Format("Inner Exception: {0}", ex.InnerException.GetType().Name));
- if (!string.IsNullOrEmpty(ex.InnerException.Message)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.Message);
- if (!string.IsNullOrEmpty(ex.InnerException.Source)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.Source);
- if (!string.IsNullOrEmpty(ex.InnerException.StackTrace)) this.WriteTrace(0, category, TraceSeverity.Unexpected, ex.InnerException.StackTrace);
- }
- #endregion
- #region Log to system debug
- string catString = string.Format("{0}:{1}:{2}", NovaDiagnosticAreaName, categoryName, msg);
- System.Diagnostics.Trace.WriteLine(string.Format("{0}: exception: {1}", msg, ex.GetType().Name), catString);
- if (!string.IsNullOrEmpty(ex.Message)) System.Diagnostics.Trace.WriteLine(ex.Message, catString);
- if (!string.IsNullOrEmpty(ex.Source)) System.Diagnostics.Trace.WriteLine(ex.Source, catString);
- if (!string.IsNullOrEmpty(ex.StackTrace)) System.Diagnostics.Trace.WriteLine(ex.StackTrace, catString);
- if (ex.InnerException != null)
- {
- System.Diagnostics.Trace.WriteLine(string.Format("Inner Exception: {0}", ex.InnerException.GetType().Name), catString);
- if (!string.IsNullOrEmpty(ex.InnerException.Message)) System.Diagnostics.Trace.WriteLine(ex.InnerException.Message, catString);
- if (!string.IsNullOrEmpty(ex.InnerException.Source)) System.Diagnostics.Trace.WriteLine(ex.InnerException.Source, catString);
- if (!string.IsNullOrEmpty(ex.InnerException.StackTrace)) System.Diagnostics.Trace.WriteLine(ex.InnerException.StackTrace, catString);
- }
- #endregion
- #region Log to Event log
- if (EventLog.SourceExists(NovaDiagnosticAreaName))
- {
- StringWriter sw = new StringWriter();
- sw.WriteLine(string.Format("{0}: exception: {1}", msg, ex.GetType().Name));
- if (!string.IsNullOrEmpty(ex.Message)) sw.WriteLine(ex.Message);
- if (!string.IsNullOrEmpty(ex.Source)) sw.WriteLine(ex.Source);
- if (!string.IsNullOrEmpty(ex.StackTrace)) sw.WriteLine(ex.StackTrace);
- EventLog.WriteEntry(NovaDiagnosticAreaName, sw.ToString());
- }
- if (ex.InnerException != null)
- {
- var exc = ex.InnerException;
- StringWriter sw = new StringWriter();
- sw.WriteLine(string.Format("{0}: exception: {1}", msg, exc.GetType().Name));
- if (!string.IsNullOrEmpty(ex.Message)) sw.WriteLine(exc.Message);
- if (!string.IsNullOrEmpty(ex.Source)) sw.WriteLine(exc.Source);
- if (!string.IsNullOrEmpty(ex.StackTrace)) sw.WriteLine(exc.StackTrace);
- EventLog.WriteEntry(NovaDiagnosticAreaName, sw.ToString());
- }
- #endregion
- }
- catch { }
- }
- #region Utils
- [MethodImpl(MethodImplOptions.NoInlining)]
- public string GetCurrentMethod()
- {
- StackTrace st = new StackTrace();
- StackFrame sf = st.GetFrame(1);
- return sf.GetMethod().Name;
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public string GetCurrentClass()
- {
- StackTrace st = new StackTrace();
- StackFrame sf = st.GetFrame(1);
- return sf.GetMethod().DeclaringType.Name;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement