3 Date Created: 08/24/2016
4 ---------------------------------------------------
8 using System.Collections.Generic;
9 using System.Diagnostics;
11 using System.Reflection;
13 using CPE.App.Notify.Models.Enums;
16 namespace CPE.App.Notify.Extensions {
17 public static class LoggingExtensions {
18 private static readonly ILog _logging = LogManager.GetLogger(new StackTrace().GetFrame(2)
22 #region private helper methods
24 // ReSharper disable once RedundantAssignment
25 private static void ignoreException(Exception exception) {
26 Console.WriteLine(exception);
29 private static string getMethodText(MethodBase methodBase = null) {
30 if(methodBase == null) {
31 methodBase = getMethodBase();
33 if(methodBase != null) {
34 var declaringType = methodBase.DeclaringType;
35 var methodName = $"{declaringType}.{methodBase.Name}";
41 private static MethodBase getMethodBase() {
42 var methodBase = getStackFrame()
47 private static StackFrame getStackFrame() {
48 var stackTrace = new StackTrace(true);
49 StackFrame stackFrame = null;
50 var frames = stackTrace.GetFrames();
53 foreach (var frame in frames) {
54 var methodName = frame.GetMethod()
56 if(methodName.ToLower() == "log") {
58 } else if((methodName.ToLower() != "log") && logFound) {
62 } catch (Exception exception) {
63 ignoreException(exception);
71 private static ParameterInfo[] getParameters(MethodBase methodBase = null) {
72 if(methodBase == null) {
73 methodBase = getMethodBase();
75 var parameters = methodBase?.GetParameters();
79 private static string getParameterText(MethodBase methodBase = null) {
80 var parameters = getParameters(methodBase);
81 if(parameters != null) {
82 var parameterText = new StringBuilder();
83 foreach (var parameter in parameters) {
85 var parameterType = parameter.ParameterType;
86 var parameterTypeSplit = parameterType.ToString()
87 .Split(Convert.ToChar("."));
88 var parameterTypeString = parameterTypeSplit.Last();
89 parameterText.AppendFormat("{0} {1}", parameterTypeString, parameter.Name);
91 if(parameter.HasDefaultValue) {
92 string defaultValue = null;
93 if(parameter.DefaultValue == null) {
94 defaultValue = "null";
96 defaultValue = parameter.DefaultValue.ToString();
99 parameterText.AppendFormat(" = {0}", defaultValue);
101 parameterText.Append(", ");
102 } catch (Exception exception) {
103 ignoreException(exception);
106 if(parameterText.ToString()
108 parameterText = parameterText.Remove(parameterText.Length - 2, 2);
110 return parameterText.ToString();
117 #region public logging methods
120 /// Universal extension for logging within the application.
122 /// <param name="value">This is the value of the object that will be logged</param>
123 /// <param name="loggingLevel">Optional log4net level with Debug</param>
124 /// <param name="message">Optional message that will be appended to log</param>
125 /// <param name="key">Optional key parameter for easy searching of log</param>
126 /// <returns></returns>
127 public static T Log<T>(this T value, LoggingLevels loggingLevel = LoggingLevels.Debug, string message = null, long? key = null) {
128 // Values for identifying class and method information
130 var logText = new StringBuilder();
133 var method = getMethodBase();
134 var methodText = getMethodText(method);
135 var parameterText = getParameterText(method);
136 var stackFrame = getStackFrame();
137 var stackFrameLineNumber = stackFrame.GetFileLineNumber();
138 string methodName = $"{methodText}({parameterText})[Line: {stackFrameLineNumber}]";
142 keyValue = $"[key:{key}]";
145 // Generate text for method information
146 logText.AppendFormat("[{0}]{1} {2}", methodName, keyValue, value);
147 } catch (Exception exception) {
148 // There was an error getting method information
149 logText.Append("[Undefined]");
150 ignoreException(exception);
153 if(message != null) {
154 logText.AppendFormat(" : {0}", message);
157 // Call log4net methods to log the message against the appenders
158 switch (loggingLevel) {
159 case LoggingLevels.Info:
160 _logging.Info(logText);
162 case LoggingLevels.Debug:
163 _logging.Debug(logText);
165 case LoggingLevels.Warn:
166 _logging.Warn(logText);
168 case LoggingLevels.Error:
169 _logging.Error(logText);
171 case LoggingLevels.Fatal:
172 _logging.Fatal(logText);
178 public static List<T> Log<T>(this List<T> values, LoggingLevels loggingLevel = LoggingLevels.Debug, string message = null) {
179 foreach (var value in values) {
181 .Log(loggingLevel, message);