CPE_learningsite
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
/* Developer: Tyler Allen Date Created: 08/24/2016 --------------------------------------------------- */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using CPE.App.Notify.Models.Enums; using log4net; namespace CPE.App.Notify.Extensions { public static class LoggingExtensions { private static readonly ILog _logging = LogManager.GetLogger(new StackTrace().GetFrame(2) .GetMethod() .DeclaringType); #region private helper methods // ReSharper disable once RedundantAssignment private static void ignoreException(Exception exception) { Console.WriteLine(exception); } private static string getMethodText(MethodBase methodBase = null) { if(methodBase == null) { methodBase = getMethodBase(); } if(methodBase != null) { var declaringType = methodBase.DeclaringType; var methodName = $"{declaringType}.{methodBase.Name}"; return methodName; } return null; } private static MethodBase getMethodBase() { var methodBase = getStackFrame() .GetMethod(); return methodBase; } private static StackFrame getStackFrame() { var stackTrace = new StackTrace(true); StackFrame stackFrame = null; var frames = stackTrace.GetFrames(); var logFound = false; foreach (var frame in frames) { var methodName = frame.GetMethod() .Name; if(methodName.ToLower() == "log") { logFound = true; } else if((methodName.ToLower() != "log") && logFound) { try { stackFrame = frame; break; } catch (Exception exception) { ignoreException(exception); } } } return stackFrame; } private static ParameterInfo[] getParameters(MethodBase methodBase = null) { if(methodBase == null) { methodBase = getMethodBase(); } var parameters = methodBase?.GetParameters(); return parameters; } private static string getParameterText(MethodBase methodBase = null) { var parameters = getParameters(methodBase); if(parameters != null) { var parameterText = new StringBuilder(); foreach (var parameter in parameters) { try { var parameterType = parameter.ParameterType; var parameterTypeSplit = parameterType.ToString() .Split(Convert.ToChar(".")); var parameterTypeString = parameterTypeSplit.Last(); parameterText.AppendFormat("{0} {1}", parameterTypeString, parameter.Name); if(parameter.HasDefaultValue) { string defaultValue = null; if(parameter.DefaultValue == null) { defaultValue = "null"; } else { defaultValue = parameter.DefaultValue.ToString(); } parameterText.AppendFormat(" = {0}", defaultValue); } parameterText.Append(", "); } catch (Exception exception) { ignoreException(exception); } } if(parameterText.ToString() .EndsWith(", ")) { parameterText = parameterText.Remove(parameterText.Length - 2, 2); } return parameterText.ToString(); } return null; } #endregion #region public logging methods /// <summary> /// Universal extension for logging within the application. /// </summary> /// <param name="value">This is the value of the object that will be logged</param> /// <param name="loggingLevel">Optional log4net level with Debug</param> /// <param name="message">Optional message that will be appended to log</param> /// <param name="key">Optional key parameter for easy searching of log</param> /// <returns></returns> public static T Log<T>(this T value, LoggingLevels loggingLevel = LoggingLevels.Debug, string message = null, long? key = null) { // Values for identifying class and method information var logText = new StringBuilder(); try { var method = getMethodBase(); var methodText = getMethodText(method); var parameterText = getParameterText(method); var stackFrame = getStackFrame(); var stackFrameLineNumber = stackFrame.GetFileLineNumber(); string methodName = $"{methodText}({parameterText})[Line: {stackFrameLineNumber}]"; var keyValue = ""; if(key.HasValue) { keyValue = $"[key:{key}]"; } // Generate text for method information logText.AppendFormat("[{0}]{1} {2}", methodName, keyValue, value); } catch (Exception exception) { // There was an error getting method information logText.Append("[Undefined]"); ignoreException(exception); } if(message != null) { logText.AppendFormat(" : {0}", message); } // Call log4net methods to log the message against the appenders switch (loggingLevel) { case LoggingLevels.Info: _logging.Info(logText); break; case LoggingLevels.Debug: _logging.Debug(logText); break; case LoggingLevels.Warn: _logging.Warn(logText); break; case LoggingLevels.Error: _logging.Error(logText); break; case LoggingLevels.Fatal: _logging.Fatal(logText); break; } return value; } public static List<T> Log<T>(this List<T> values, LoggingLevels loggingLevel = LoggingLevels.Debug, string message = null) { foreach (var value in values) { value.ToString() .Log(loggingLevel, message); } return values; } #endregion } } |
Commits for CPE_learningsiteCPE/CPE.App/CPE.App.Notify/Extensions/LoggingExtensions.cs
Revision | Author | Commited | Message |
---|---|---|---|
4cd176 ... | v.shishlov | Fri 27 Aug, 2021 14:33:17 +0000 | initial commit |