Beispiel #1
0
        public DurationLogger(string operationName, LogSource logSource, bool isEnabled)
        {
            Helpers.Argument.ValidateIsNotNull(logSource, nameof(logSource));
            Helpers.Argument.ValidateIsNotNullOrWhitespace(operationName, nameof(operationName));

            _operationName = operationName;
            _logSource     = logSource;

            // We enable the duration logging feature to be easily switched off via flag.
            if (!isEnabled)
            {
                return;
            }

            _stopwatch = new Stopwatch();
            _stopwatch.Start();

            _logSource.Debug($"{_operationName} - started.");
        }
Beispiel #2
0
        public ChildLogSource(string name, LogSource parent) : base(name)
        {
            Helpers.Argument.ValidateIsNotNull(parent, nameof(parent));

            _parent = parent;
        }
Beispiel #3
0
 public DurationLogger(string operationName, LogSource logSource) : this(operationName, logSource, true)
 {
 }
Beispiel #4
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);
            }
        }