public ContextSummary Build()
        {
            if (_rootNode.Messages.Count == 0)
            {
                return(null);
            }

            var info = new BuildInfo(_formatter.GetDetailsHeader());

            var contextStart = _contextTime.StartedAt;

            ProcessNode(_rootNode, null, contextStart, 0, info);

            var now = _contextTime.Now;
            var sinceLastMessage = now.Subtract(info.LastMessage.CreatedAt);

            info.Durations.Add((int)sinceLastMessage.TotalMilliseconds);
            info.DetailsBuilder.Append(_formatter.GetDetailsFooter(sinceLastMessage));

            var result = new ContextSummary
            {
                Level      = info.MaxLevel,
                Details    = info.DetailsBuilder.ToString(),
                Exceptions = info.Exceptions,
                Durations  = info.Durations,
                Loggers    = info.Loggers
            };

            var last = info.LastMessage?.OriginalMessage;

            result.Message = Trim(last, MessageLengthLimit);

            return(result);
        }
        private static void ApplyContext(LogCastDocument document, LogCastContext context, ContextSummary summary)
        {
            document.AddProperty(Property.Timestamp, context.StartedAt);
            document.AddProperty(Property.CorrelationId, context.CorrelationId, true);
            document.AddProperty(Property.OperationName, context.OperationName, true);

            if (summary != null)
            {
                document.AddProperty(Property.LogLevel, summary.Level.ToString());
                document.AddProperty(Property.LogLevelCode, (int)summary.Level);
                document.AddProperty(Property.Message, summary.Message);
                document.AddProperty(Property.Details, summary.Details);
                document.AddProperty(Property.Durations.Name, Property.DefaultChildName, summary.Durations);
                document.AddProperty(Property.LoggerName, summary.Loggers);

                ApplyError(document, summary.Exceptions);
            }
        }