예제 #1
0
        internal static void LogFunctionResult(this ILogger logger, FunctionInstanceLogEntry logEntry)
        {
            bool succeeded = logEntry.Exception == null;

            // build the string and values
            string result    = succeeded ? "Succeeded" : "Failed";
            string logString = $"Executed '{{{LogConstants.FullNameKey}}}' ({result}, Id={{{LogConstants.InvocationIdKey}}})";

            object[] values = new object[]
            {
                logEntry.FunctionName,
                logEntry.FunctionInstanceId
            };

            // generate additional payload that is not in the string
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add(LogConstants.NameKey, logEntry.LogName);
            properties.Add(LogConstants.TriggerReasonKey, logEntry.TriggerReason);
            properties.Add(LogConstants.StartTimeKey, logEntry.StartTime);
            properties.Add(LogConstants.EndTimeKey, logEntry.EndTime);
            properties.Add(LogConstants.DurationKey, logEntry.Duration);
            properties.Add(LogConstants.SucceededKey, succeeded);

            FormattedLogValuesCollection payload = new FormattedLogValuesCollection(logString, values, new ReadOnlyDictionary <string, object>(properties));
            LogLevel level = succeeded ? LogLevel.Information : LogLevel.Error;

            logger.Log(level, 0, payload, logEntry.Exception, (s, e) => s.ToString());
        }
        public void NoString(string message)
        {
            var additionalPayload = new Dictionary <string, object>()
            {
                ["additional"] = "abc",
                ["payload"]    = 123
            };

            var payload = new FormattedLogValuesCollection(message,
                                                           null, new ReadOnlyDictionary <string, object>(additionalPayload));

            // Verify string behavior
            // FormattedLogValues changes nulls to [null]
            var expectedMessage = message ?? "[null]";

            Assert.Equal(expectedMessage, payload.ToString());

            // Verify the collection
            var expectedPayload = new Dictionary <string, object>
            {
                ["additional"] = "abc",
                ["payload"]    = 123,

                // FormattedLogValues adds this automatically to capture the string template
                ["{OriginalFormat}"] = expectedMessage
            };

            ValidateCollection(expectedPayload, payload);
        }
        internal static void LogFunctionResultAggregate(this ILogger logger, FunctionResultAggregate resultAggregate)
        {
            // we won't output any string here, just the data
            FormattedLogValuesCollection payload = new FormattedLogValuesCollection(string.Empty, null, resultAggregate.ToReadOnlyDictionary());

            logger.Log(LogLevel.Information, 0, payload, null, (s, e) => s.ToString());
        }
        public void NoStringPayload()
        {
            var additionalPayload = new Dictionary <string, object>()
            {
                ["additional"] = "abc",
                ["payload"]    = 123
            };

            var payload = new FormattedLogValuesCollection("This string has no data",
                                                           null, new ReadOnlyDictionary <string, object>(additionalPayload));

            // Verify string behavior
            Assert.Equal("This string has no data", payload.ToString());

            // Verify the collection
            var expectedPayload = new Dictionary <string, object>
            {
                ["additional"] = "abc",
                ["payload"]    = 123,

                // FormattedLogValues adds this automatically to capture the string template
                ["{OriginalFormat}"] = "This string has no data"
            };

            ValidateCollection(expectedPayload, payload);
        }
        public void Verify_ToString_AndCollection()
        {
            var additionalPayload = new Dictionary <string, object>()
            {
                ["additional"] = "abc",
                ["payload"]    = 123
            };

            var payload = new FormattedLogValuesCollection("This {string} has some {data}",
                                                           new object[] { "xyz", 789 }, new ReadOnlyDictionary <string, object>(additionalPayload));

            // Verify string behavior
            Assert.Equal("This xyz has some 789", payload.ToString());

            // Verify the collection
            var expectedPayload = new Dictionary <string, object>
            {
                ["additional"] = "abc",
                ["payload"]    = 123,
                ["string"]     = "xyz",
                ["data"]       = 789,

                // FormattedLogValues adds this automatically to capture the string template
                ["{OriginalFormat}"] = "This {string} has some {data}"
            };

            ValidateCollection(expectedPayload, payload);
        }
        public void NullValues_WithCurlyBraces()
        {
            var additionalPayload = new Dictionary <string, object>()
            {
                ["additional"] = "abc",
                ["payload"]    = 123
            };

            var message = "{this} {should} {work} {";
            var payload = new FormattedLogValuesCollection(message,
                                                           null, new ReadOnlyDictionary <string, object>(additionalPayload));

            // Verify string behavior
            Assert.Equal(message, payload.ToString());

            // Verify the collection
            var expectedPayload = new Dictionary <string, object>
            {
                ["additional"] = "abc",
                ["payload"]    = 123,

                // FormattedLogValues adds this automatically to capture the string template
                ["{OriginalFormat}"] = message
            };

            ValidateCollection(expectedPayload, payload);
        }
예제 #7
0
            /// <inheritdoc />
            public override void Trace(TraceEvent traceEvent)
            {
                if (traceEvent == null)
                {
                    throw new ArgumentNullException(nameof(traceEvent));
                }

                if (traceEvent.Level > Level)
                {
                    return;
                }

                LogLevel level = GetLogLevel(traceEvent.Level);
                FormattedLogValuesCollection logState = new FormattedLogValuesCollection(traceEvent.Message, null, new ReadOnlyDictionary <string, object>(traceEvent.Properties));

                _logger.Log(level, 0, logState, traceEvent.Exception, (s, e) => s.ToString());
            }
        // We want the short name for use with Application Insights.
        internal static void LogFunctionResult(this ILogger logger, string shortName, FunctionInstanceLogEntry logEntry, TimeSpan duration, Exception exception = null)
        {
            bool succeeded = exception == null;

            // build the string and values
            string result    = succeeded ? "Succeeded" : "Failed";
            string logString = $"Executed '{{{LoggingKeys.FullName}}}' ({result}, Id={{{LoggingKeys.InvocationId}}})";

            object[] values = new object[]
            {
                logEntry.FunctionName,
                logEntry.FunctionInstanceId
            };

            // generate additional payload that is not in the string
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add(LoggingKeys.Name, shortName);
            properties.Add(LoggingKeys.TriggerReason, logEntry.TriggerReason);
            properties.Add(LoggingKeys.StartTime, logEntry.StartTime);
            properties.Add(LoggingKeys.EndTime, logEntry.EndTime);
            properties.Add(LoggingKeys.Duration, duration);
            properties.Add(LoggingKeys.Succeeded, succeeded);

            if (logEntry.Arguments != null)
            {
                foreach (var arg in logEntry.Arguments)
                {
                    properties.Add(LoggingKeys.ParameterPrefix + arg.Key, arg.Value);
                }
            }

            FormattedLogValuesCollection payload = new FormattedLogValuesCollection(logString, values, new ReadOnlyDictionary <string, object>(properties));
            LogLevel level = succeeded ? LogLevel.Information : LogLevel.Error;

            logger.Log(level, 0, payload, exception, (s, e) => s.ToString());
        }