aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Logging/LogRow.cs
blob: 39c69eb45486618044759d3e5165d4f5af3b78b0 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Text;

namespace MediaBrowser.Common.Logging
{
    public struct LogRow
    {

        const string TimePattern = "h:mm:ss.fff tt d/M/yyyy";


        public LogSeverity Severity { get; set; }
        public string Message { get; set; }
        public string Category { get; set; }
        public int ThreadId { get; set; }
        public string ThreadName { get; set; }
        public DateTime Time { get; set; }

        public string ShortMessage
        {
            get
            {
                var message = Message;
                if (message.Length > 120)
                {
                    message = Message.Substring(0, 120).Replace(Environment.NewLine, " ") + " ... ";
                }
                return message;
            }
        }

        public string FullDescription
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Time: {0}", Time);
                sb.AppendLine();
                sb.AppendFormat("Thread Id: {0} {1}", ThreadId, ThreadName);
                sb.AppendLine();
                sb.AppendLine(Message);
                return sb.ToString();
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(Time.ToString(TimePattern))
                .Append(" , ")
                .Append(Enum.GetName(typeof(LogSeverity), Severity))
                .Append(" , ")
                .Append(Encode(Message))
                .Append(" , ")
                .Append(Encode(Category))
                .Append(" , ")
                .Append(ThreadId)
                .Append(" , ")
                .Append(Encode(ThreadName));
            return builder.ToString();
        }

        private string Encode(string str)
        {
            return (str ?? "").Replace(",", ",,").Replace(Environment.NewLine, " [n] ");
        }

        public static LogRow FromString(string message)
        {
            var split = splitString(message);
            return new LogRow()
            {
                Time = DateTime.ParseExact(split[0], TimePattern, null),
                Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), split[1]),
                Message = split[2],
                Category = split[3],
                ThreadId = int.Parse(split[4]),
                ThreadName = split[5]
            };
        }

        static string[] splitString(string message)
        {
            List<string> items = new List<string>();
            bool gotComma = false;

            StringBuilder currentItem = new StringBuilder();

            foreach (var chr in message)
            {

                if (chr == ',' && gotComma)
                {
                    gotComma = false;
                    currentItem.Append(',');
                }
                else if (chr == ',')
                {
                    gotComma = true;
                }
                else if (gotComma)
                {
                    items.Add(currentItem.ToString().Replace(" [n] ", Environment.NewLine).Trim());
                    currentItem = new StringBuilder();
                    gotComma = false;
                }
                else
                {
                    currentItem.Append(chr);
                }

            }
            items.Add(currentItem.ToString().Replace("[n]", Environment.NewLine).Trim());
            return items.ToArray();
        }
    }
}