|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Specialized; |
| 4 | +using System.Globalization; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.Versioning; |
| 7 | +using System.Security; |
| 8 | +using System.Security.Principal; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | +namespace NetCoreStack.Hisar |
| 12 | +{ |
| 13 | + public static class ExceptionFormatter |
| 14 | + { |
| 15 | + private static string LineSeparator = "======================================"; |
| 16 | + |
| 17 | + private static NameValueCollection CollectAdditionalInfo() |
| 18 | + { |
| 19 | + NameValueCollection nameValueCollection = null; |
| 20 | + NameValueCollection additionalInfo = new NameValueCollection(); |
| 21 | + if (additionalInfo["MachineName:"] == null) |
| 22 | + { |
| 23 | + additionalInfo.Add("MachineName:", string.Concat("MachineName: ", GetMachineName())); |
| 24 | + DateTime utcNow = DateTime.UtcNow; |
| 25 | + additionalInfo.Add("TimeStamp:", string.Concat("TimeStamp: ", utcNow.ToString(CultureInfo.CurrentCulture))); |
| 26 | + additionalInfo.Add("FullName:", string.Concat("FullName: ", Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName)); |
| 27 | + additionalInfo.Add("ApplicationName:", string.Concat("ApplicationName: ", Assembly.GetEntryAssembly().GetName().Name)); |
| 28 | + additionalInfo.Add("WindowsIdentity:", string.Concat("WindowsIdentity: ", GetWindowsIdentity())); |
| 29 | + nameValueCollection = additionalInfo; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + nameValueCollection = null; |
| 34 | + } |
| 35 | + return nameValueCollection; |
| 36 | + } |
| 37 | + |
| 38 | + private static string GetMachineName() |
| 39 | + { |
| 40 | + string machineName = null; |
| 41 | + try |
| 42 | + { |
| 43 | + machineName = Environment.GetEnvironmentVariable("COMPUTERNAME"); |
| 44 | + } |
| 45 | + catch (SecurityException) |
| 46 | + { |
| 47 | + machineName = "Permission Denied"; |
| 48 | + } |
| 49 | + return machineName; |
| 50 | + } |
| 51 | + |
| 52 | + private static string GetWindowsIdentity() |
| 53 | + { |
| 54 | + string name = null; |
| 55 | + try |
| 56 | + { |
| 57 | + WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); |
| 58 | + if (windowsIdentity == null) |
| 59 | + { |
| 60 | + name = null; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + name = windowsIdentity.Name; |
| 65 | + } |
| 66 | + } |
| 67 | + catch (SecurityException) |
| 68 | + { |
| 69 | + name = "Permission Denied"; |
| 70 | + } |
| 71 | + return name; |
| 72 | + } |
| 73 | + |
| 74 | + private static void ProcessAdditionalInfo(PropertyInfo propinfo, object propValue, StringBuilder stringBuilder) |
| 75 | + { |
| 76 | + if (!(propinfo.Name == "AdditionalInformation")) |
| 77 | + { |
| 78 | + stringBuilder.AppendFormat("" + Environment.NewLine + "{0}: {1}", propinfo.Name, propValue); |
| 79 | + } |
| 80 | + else if (propValue != null) |
| 81 | + { |
| 82 | + NameValueCollection currAdditionalInfo = (NameValueCollection)propValue; |
| 83 | + if (currAdditionalInfo.Count > 0) |
| 84 | + { |
| 85 | + stringBuilder.AppendFormat("" + Environment.NewLine + "AdditionalInformation:", new object[1]); |
| 86 | + int i = 0; |
| 87 | + while (i < currAdditionalInfo.Count) |
| 88 | + { |
| 89 | + stringBuilder.AppendFormat("" + Environment.NewLine + "{0}: {1}", currAdditionalInfo.GetKey(i), currAdditionalInfo[i]); |
| 90 | + i = i + 1; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static void ReflectException(Exception currException, StringBuilder strEventInfo) |
| 97 | + { |
| 98 | + object propValue = null; |
| 99 | + PropertyInfo[] properties = currException.GetType().GetProperties(); |
| 100 | + int ınt32 = 0; |
| 101 | + while (ınt32 < Convert.ToInt32(properties.Length)) |
| 102 | + { |
| 103 | + PropertyInfo propinfo = properties[ınt32]; |
| 104 | + if (propinfo.Name == "InnerException" ? false : propinfo.Name != "StackTrace") |
| 105 | + { |
| 106 | + if (!propinfo.CanRead ? false : Convert.ToInt32(propinfo.GetIndexParameters().Length) == 0) |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + propValue = propinfo.GetValue(currException, null); |
| 111 | + } |
| 112 | + catch (TargetInvocationException) |
| 113 | + { |
| 114 | + propValue = "Access failed"; |
| 115 | + } |
| 116 | + if (propValue != null) |
| 117 | + { |
| 118 | + ProcessAdditionalInfo(propinfo, propValue, strEventInfo); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + strEventInfo.AppendFormat("" + Environment.NewLine + "{0}: NULL", propinfo.Name); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + ınt32 = ınt32 + 1; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static string InternalGetMessage(Exception exception) |
| 131 | + { |
| 132 | + StringBuilder eventInformation = new StringBuilder(); |
| 133 | + NameValueCollection additionalInfo = CollectAdditionalInfo(); |
| 134 | + foreach (string info in additionalInfo) |
| 135 | + { |
| 136 | + eventInformation.AppendFormat("" + Environment.NewLine + "--> {0}", additionalInfo.Get(info)); |
| 137 | + } |
| 138 | + if (exception != null) |
| 139 | + { |
| 140 | + Exception currException = exception; |
| 141 | + do |
| 142 | + { |
| 143 | + eventInformation.AppendFormat("" + Environment.NewLine + "" + Environment.NewLine + "{0}" + Environment.NewLine + "{1}", "Exception Information Details:", LineSeparator); |
| 144 | + eventInformation.AppendFormat("" + Environment.NewLine + "{0}: {1}", "Exception Type", currException.GetType().FullName); |
| 145 | + ReflectException(currException, eventInformation); |
| 146 | + if (currException.StackTrace != null) |
| 147 | + { |
| 148 | + eventInformation.AppendFormat("" + Environment.NewLine + "" + Environment.NewLine + "{0} " + Environment.NewLine + "{1}", "StackTrace Information Details:", LineSeparator); |
| 149 | + eventInformation.AppendFormat("" + Environment.NewLine + "{0}", currException.StackTrace); |
| 150 | + } |
| 151 | + currException = currException.InnerException; |
| 152 | + } while (currException != null); |
| 153 | + } |
| 154 | + return eventInformation.ToString(); |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// |
| 159 | + /// </summary> |
| 160 | + /// <param name="exception"></param> |
| 161 | + /// <param name="values">Dictionary values</param> |
| 162 | + /// <returns></returns> |
| 163 | + public static string CreateMessage(Exception exception, IDictionary<string, object> values = null) |
| 164 | + { |
| 165 | + var resultString = new StringBuilder(); |
| 166 | + if (values != null) |
| 167 | + { |
| 168 | + resultString.AppendFormat("AdditionalInfo:"); |
| 169 | + foreach (KeyValuePair<string, object> item in values) |
| 170 | + { |
| 171 | + resultString.AppendFormat("" + Environment.NewLine + "--> {0}: {1}", item.Key, item.Value); |
| 172 | + } |
| 173 | + resultString.AppendLine(Environment.NewLine + LineSeparator); |
| 174 | + } |
| 175 | + |
| 176 | + resultString.AppendLine(InternalGetMessage(exception)); |
| 177 | + return resultString.ToString(); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments