aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs
blob: 7c325ee19b769488662938891d7152e750ea51b0 (plain)
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Logging
{
    /// <summary>
    /// Represents an exception log item.
    /// </summary>
    [Serializable]
    public class ExceptionLogItem : LogItemBase
    {
        /// <summary>
        /// Gets or sets the log item exception.
        /// </summary>
        public Exception Exception { get; set; }

        /// <summary>
        /// Gets or sets the error description.
        /// </summary>
        public String Description { get; set; }

        /// <summary>
        /// Gets the log message.
        /// </summary>
        public override string Message
        {
            get
            {
                if (Exception is AggregateException)
                {
                    try
                    {
                        String message = String.Empty;

                        if (Description != null)
                        {
                            message += Description + Environment.NewLine;
                        }

                        message += String.Join(Environment.NewLine, (Exception as AggregateException).InnerExceptions.Select(x => x.Message));

                        return message;
                    }
                    catch
                    {
                        return Exception.Message;
                    }
                }
                else
                {
                    return Exception.Message;
                }
            }
            set { }
        }

        /// <summary>
        /// Returns a formatted string of the log item.
        /// </summary>
        public override string ToString()
        {
            return String.Format("[{0}] [{6}] [{1}] [{2}] [{3}]: {4}{5}", TimeStamp.ToString("HH:mm:ss.ff"), GetRelativeCallerFilePath(), CallerMethodName, CallerLineNumber, Description, Environment.NewLine + Exception.FlattenException(), Category);
        }

    }
}