Exemple #1
0
 /// <summary>
 /// Logs any exceptions from the task to the given log source.
 /// </summary>
 public static Task LogExceptions(this Task task, LogSource toLog)
 {
     return(task.ContinueWith(t =>
     {
         if (t.Exception != null)
         {
             toLog.Error(t.Exception.ToString());
         }
     }));
 }
Exemple #2
0
        /// <summary>
        /// Logs any exceptions from the task to the given log source.
        /// </summary>
        public static Task <T> LogExceptions <T>(this Task <T> task, LogSource toLog)
        {
            return(task.ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    toLog.Error(t.Exception.ToString());
                }

                return t.Result;
            }));
        }
Exemple #3
0
        /// <summary>
        /// Logs an error report with the logging system.
        /// </summary>
        /// <param name="exception">An exception that describes error that occurred.</param>
        /// <param name="additionalData">Any additional data you wish to attach to the report.</param>
        /// <param name="log">The log to write the output to. A suitable default will be selected if null.</param>
        public static void Log(Exception exception, object additionalData = null, LogSource log = null)
        {
            Helpers.Argument.ValidateIsNotNull(exception, nameof(exception));

            log = log ?? Toolkit.Log.Default.CreateChildSource(nameof(ErrorReport));
            try
            {
                var details = new StringBuilder();

                // First a "subject line" suitable for summarizing and email headering purposes.
                // We take the last (deepest) exception from the stack, as it is the root cause of the exception.
                details.AppendLine(Summarize(exception));

                details.AppendLine();
                details.AppendFormat("Timestamp: {0}", DateTimeOffset.UtcNow.ToString("u"));
                details.AppendLine();
                details.AppendFormat("Machine name: {0}", Environment.MachineName);
                details.AppendLine();
                details.AppendFormat("Operating system: {0}", Environment.OSVersion);
                details.AppendLine();
                details.AppendFormat("Current user: {0}\\{1}", Environment.UserDomainName, Environment.UserName);
                details.AppendLine();
                details.AppendFormat("CLR version: {0}", Environment.Version);
                details.AppendLine();

                details.AppendLine();
                details.AppendLine("Loaded non-system assemblies:");
                var loadedAssemblyNames = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName());

                // We filter out system assemblies because they are boring.
                foreach (var assemblyName in loadedAssemblyNames
                         .Where(a => !a.Name.StartsWith("System."))
                         .OrderBy(a => a.Name))
                {
                    details.AppendLine($"{assemblyName.Name} {assemblyName.Version}");
                }

                details.AppendLine();
                details.AppendLine("Exception message stack:");

                // We want the deepest exceptions first, as they are the most important.
                var exceptionStack = FlattenException(exception).Reverse().ToList();

                for (int i = 0; i < exceptionStack.Count; i++)
                {
                    details.AppendLine($"{i + 1}. {exceptionStack[i]}");
                }

                details.AppendLine();

                if (additionalData != null)
                {
                    details.AppendLine();
                    details.AppendLine("Additional data:");
                    details.AppendLine(Helpers.Debug.ToDebugString(additionalData));

                    details.AppendLine();
                }

                log.Error(details.ToString());
            }
            catch (Exception ex)
            {
                log.Wtf("Exception occurred when attempting to create error report! " + ex);
            }
        }