示例#1
0
 public StructuredTextLogger(string path, IAnalytics analytics)
 {
     _depth     = 0;
     _path      = path;
     _analytics = analytics;
     _structuredTextDocument = new StructuredTextDocument <LogEntry>(path,
                                                                     // DateTime.ToString("o") => "2015-08-04T00:08:38.5489308Z"
                                                                     e => string.Join(LogEntrySeparator, e.LogTime.ToString("o"), e.Message, e.Id, (int)e.Type),
                                                                     str =>
     {
         var splitted = str.Split(new[] { LogEntrySeparator }, StringSplitOptions.None);
         if (splitted.Length == 4)
         {
             var time    = DateTime.Parse(splitted[0]).ToUniversalTime();
             var message = UnsanitizeValue(splitted[1]);
             var id      = splitted[2];
             var type    = (LogEntryType)Int32.Parse(splitted[3]);
             return(new LogEntry(time, id, message, type));
         }
         else
         {
             throw new FormatException(string.Format("the log line \"{0}\" is in an invalid format", str));
         }
     });
 }
        private IEnumerable <StructuredTextDocumentEntry <T> > ParseLines(string[] lines, int startIndex, int depth)
        {
            List <StructuredTextDocumentEntry <T> > collection = new List <StructuredTextDocumentEntry <T> >();
            StructuredTextDocumentEntry <T>         logEntry   = null;

            for (var i = startIndex; i < lines.Length; i++)
            {
                //skip empty lines
                if (string.IsNullOrWhiteSpace(lines[i]))
                {
                    continue;
                }

                var entryDepth = StructuredTextDocument.GetEntryDepth(lines[i]);
                if (entryDepth == depth)
                {
                    logEntry = new StructuredTextDocumentEntry <T>
                    {
                        LogEntry = _deserializer(lines[i].Substring(depth)),
                        Children = Enumerable.Empty <StructuredTextDocumentEntry <T> >()
                    };
                    collection.Add(logEntry);
                }
                else if (entryDepth > depth)
                {
                    if (logEntry == null)
                    {
                        throw new FormatException(string.Format("log file '{0}' is in an invalid format", _path));
                    }

                    logEntry.Children = ParseLines(lines, i, entryDepth);
                    i += (logEntry.Children.Count() - 1);
                }
                else if (entryDepth < depth)
                {
                    return(collection);
                }
            }
            return(collection);
        }