Ejemplo n.º 1
0
        private static string GetLog(
            LogFormatter formatter,
            string message,
            DirectSubmissionLogLevel level,
            Dictionary <string, string> properties)
        {
            var sb = new StringBuilder();

            formatter.FormatLog(
                sb,
                sb, // not used here
                DateTime.UtcNow,
                message,
                null,
                level.GetName(),
                exception: null,
                RenderPropertiesDelegate);
            return(sb.ToString());

            LogPropertyRenderingDetails RenderPropertiesDelegate(JsonTextWriter writer, in StringBuilder stringBuilder)
            {
                foreach (var pair in properties)
                {
                    writer.WritePropertyName(pair.Key);
                    writer.WriteValue(pair.Value);
                }

                return(new LogPropertyRenderingDetails(false, false, false, false, false, false, message));
            }
        }
Ejemplo n.º 2
0
 public static void FormatLogEvent(LogFormatter logFormatter, StringBuilder sb, ILoggingEventDuckBase logEntry, DateTime timestamp)
 {
     logFormatter.FormatLog(
         sb,
         logEntry,
         timestamp,
         logEntry.RenderedMessage,
         eventId: null,
         logEntry.Level.ToStandardLevelString(),
         exception: null, // We can't pass the exception here, as it might be null if the event has been serialized
         (JsonTextWriter w, in ILoggingEventDuckBase e) => RenderProperties(w, in e));
 }
Ejemplo n.º 3
0
        public static void FormatLogEvent(LogFormatter logFormatter, StringBuilder sb, ILogEvent logEvent)
        {
            var message = logEvent.RenderMessage();

            logFormatter.FormatLog(
                sb,
                logEvent,
                logEvent.Timestamp.UtcDateTime,
                message,
                eventId: null,
                GetLogLevelString(logEvent.Level),
                logEvent.Exception,
                (JsonTextWriter w, in ILogEvent e) => RenderProperties(w, e));
        }
Ejemplo n.º 4
0
        public void WritesLogFormatCorrectly(
            bool hasRenderedSource,
            bool hasRenderedService,
            bool hasRenderedHost,
            bool hasRenderedTags,
            bool hasRenderedEnv,
            bool hasRenderedVersion)
        {
            var timestamp = new DateTime(year: 2020, month: 3, day: 7, hour: 11, minute: 23, second: 26, millisecond: 500, DateTimeKind.Utc);
            var sb        = new StringBuilder();
            var state     = new TestObject();
            var message   = "Some message";
            var logLevel  = "Info";

            _formatter.FormatLog(sb, state, timestamp, message, eventId: null, logLevel, exception: null, RenderProperties);

            var log = sb.ToString();

            log
            .Should()
            .Contain(timestamp.ToString("o"))
            .And.Contain(message)
            .And.Contain(logLevel);

            using var scope = new AssertionScope();
            HasExpectedValue(log, !hasRenderedSource, $"\"ddsource\":\"{Source}\"");
            HasExpectedValue(log, !hasRenderedService, $"\"service\":\"{Service}\"");
            HasExpectedValue(log, !hasRenderedHost, $"\"host\":\"{Host}\"");
            HasExpectedValue(log, !hasRenderedTags, $"\"ddtags\":\"Key1:Value1;Key2:Value2\"");
            HasExpectedValue(log, !hasRenderedEnv, $"\"dd_env\":\"{Env}\"");
            HasExpectedValue(log, !hasRenderedVersion, $"\"dd_version\":\"{Version}\"");

            LogPropertyRenderingDetails RenderProperties(JsonTextWriter jsonTextWriter, in TestObject o) =>
            new LogPropertyRenderingDetails(
                hasRenderedSource: hasRenderedSource,
                hasRenderedService: hasRenderedService,
                hasRenderedHost: hasRenderedHost,
                hasRenderedTags: hasRenderedTags,
                hasRenderedEnv: hasRenderedEnv,
                hasRenderedVersion: hasRenderedVersion,
                messageTemplate: null);
            public override void Format(StringBuilder sb, LogFormatter formatter)
            {
                formatter.FormatLog <Context?>(
                    sb,
                    _context,
                    DateTime.UtcNow,
                    _message,
                    eventId: null,
                    logLevel: DirectSubmissionLogLevelExtensions.Information,
                    exception: null,
                    (JsonTextWriter writer, in Context? state) =>
                {
                    if (state.HasValue)
                    {
                        writer.WritePropertyName("dd.trace_id");
                        writer.WriteValue($"{state.Value.TraceId}");
                        writer.WritePropertyName("dd.span_id");
                        writer.WriteValue($"{state.Value.SpanId}");
                        writer.WritePropertyName("_dd.origin");
                        writer.WriteValue($"{state.Value.Origin}");
                    }

                    return(default);