public static void LogException(Exception exception) { var innerExceptionItem = exception.InnerException; var sb = new StringBuilder(); sb.AppendLine(string.Format("------------#Exception - {0} -----------------------------------------------------------------------------------", DateTime.Now.ToString())); sb.AppendLine(exception.GetErrorMessage()).AppendLine(); var innerExceptionCount = 0; while (innerExceptionItem != null) { innerExceptionCount++; sb .AppendLine(string.Format("Inner Exception #{0}: \n{1}\n\n Stack : \n{2}\n", innerExceptionCount, innerExceptionItem.Message, innerExceptionItem.StackTrace.ToString())); innerExceptionItem = innerExceptionItem.InnerException; } sb.AppendLine(); File.AppendAllText("LogFile.txt", sb.ToString(), Encoding.UTF8); }
public static MessageBoxResult ShowException(Exception exception) { return MessageBox.Show(exception.GetErrorMessage(), "Error", MessageBoxButton.OK, MessageBoxImage.Error); }
public void Error(Exception exception) { Error(exception, exception.GetErrorMessage()); }
public void SetExceptionAddtionalInformation(Exception exception) { var exceptionStack = new Stack<Exception>(); // create exception stack for (Exception e = exception; e != null; e = e.InnerException) { exceptionStack.Push(e); } if (this.ErrorStackData == null) { this.ErrorStackData = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); } try { this.ErrorStackData["ExceptionSource"] = exception.GetErrorSource(); this.ErrorStackData["ExceptionMessage"] = exception.GetErrorMessage(); this.ErrorStackData["ExceptionTypeName"] = exception.GetErrorTypeName(); this.ErrorStackData["ExceptionHelpLink"] = exception.HelpLink; if (exceptionStack.Count != 0) { // render exception type and message Exception ex = exceptionStack.Pop(); // Load stack trace var stackTrace = new StackTrace(ex, true); for (int frame = 0; frame < stackTrace.FrameCount; frame++) { StackFrame stackFrame = stackTrace.GetFrame(frame); MethodBase method = stackFrame.GetMethod(); Type declaringType = method.DeclaringType; if (declaringType != typeof(Ensure) && stackFrame.GetFileLineNumber() > 0) { this.ErrorStackData["HostingEnvironmentApplicationPhysicalPath"] = HostingEnvironment.ApplicationPhysicalPath; this.ErrorStackData["HttpRuntimeAppDomainAppId"] = HttpRuntime.AppDomainAppId; this.ErrorStackData["EnvironmentMachineName"] = Environment.MachineName; this.ErrorStackData["CurrentCultureDisplayName"] = Thread.CurrentThread.CurrentCulture.DisplayName; if (declaringType != null) { this.ErrorStackData["TypeGUID"] = declaringType.GUID.ToShortId(); this.ErrorStackData["TypeFullName"] = declaringType.FullName; this.ErrorStackData["TypeAssemblyQualifiedName"] = declaringType.AssemblyQualifiedName; this.ErrorStackData["TypeNamespace"] = declaringType.Namespace; this.ErrorStackData["AssemblyName"] = Path.GetFileName(declaringType.Assembly.Location); } var csfilename = stackFrame.GetFileName(); if (!string.IsNullOrWhiteSpace(csfilename)) { this.ErrorStackData["StackFrameFileName"] = stackFrame.GetFileName(); this.ErrorStackData["StackFrameFileNameVirtualPath"] = csfilename.Replace(HostingEnvironment.ApplicationPhysicalPath, "/").Replace('\\', '/'); } this.ErrorStackData["StackFrameFileLineNumber"] = stackFrame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture); this.ErrorStackData["StackFrameShortFileName"] = Path.GetFileName(stackFrame.GetFileName()); var trace = new StringBuilder(64); if (declaringType != null) { trace.Append(declaringType.Namespace).Append('.').Append(declaringType.Name).Append('.').Append(method.Name); } this.ErrorStackData["MethodBaseName"] = trace.ToString(); trace.Append('('); // get parameter information ParameterInfo[] parameters = method.GetParameters(); for (int paramIndex = 0; paramIndex < parameters.Length; paramIndex++) { trace.Append(((paramIndex != 0) ? "," : string.Empty) + parameters[paramIndex].ParameterType.Name + " " + parameters[paramIndex].Name); } trace.Append(')'); this.ErrorStackData["MethodBaseFullName"] = trace.ToString(); break; } } } } catch (Exception ex) { VLog.Logger.InfoException("GetExceptionAddtionalInformation", ex); } }