From b50f78e5da6f3fdfc59e577ca61b88771da7d211 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 12 Jul 2012 02:55:27 -0400 Subject: Initial check-in --- MediaBrowser.Common/Logging/LogRow.cs | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 MediaBrowser.Common/Logging/LogRow.cs (limited to 'MediaBrowser.Common/Logging/LogRow.cs') diff --git a/MediaBrowser.Common/Logging/LogRow.cs b/MediaBrowser.Common/Logging/LogRow.cs new file mode 100644 index 000000000..39c69eb45 --- /dev/null +++ b/MediaBrowser.Common/Logging/LogRow.cs @@ -0,0 +1,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 items = new List(); + 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(); + } + } +} -- cgit v1.2.3