Example #1
0
        public void Log_WithComplexMicrosoftLoggerScopeAndException_LogsValidEvent()
        {
            var scope     = new { id = 1, name = 2 };
            var exception = new Exception("exception");

            var expectedLogEvent = new MicrosoftMemoryLoggerEvent
            {
                Category  = TestCategoryName,
                LogLevel  = MsLogLevel.Information,
                Message   = "message",
                Exception = exception,
                State     = new ImmutableArrayDictionary <string, object>(
                    new Dictionary <string, object>
                {
                    [MicrosoftLogProperties.OriginalFormatKey] = "message"
                },
                    StringComparer.Ordinal)
            };

            using (microsoftLogger.BeginScope(scope))
            {
                microsoftLog.Info(new Exception("exception"), "message");

                microsoftLogger.Events.Single().Should().BeEquivalentTo(expectedLogEvent);
            }
        }
Example #2
0
        public void Log_WithMultiplyMicrosoftLoggerScopes_LogsValidEvent()
        {
            const string scope1 = "s1";
            const string scope2 = "s2";

            var expectedLogEvent = new MicrosoftMemoryLoggerEvent
            {
                Category = TestCategoryName,
                LogLevel = MsLogLevel.Information,
                Message  = "message",
                State    = new ImmutableArrayDictionary <string, object>(
                    new Dictionary <string, object>
                {
                    [MicrosoftLogProperties.OriginalFormatKey] = "message"
                },
                    StringComparer.Ordinal)
            };

            using (microsoftLogger.BeginScope(scope1))
                using (microsoftLogger.BeginScope(scope2))
                {
                    microsoftLog.Info("message");

                    microsoftLogger.Events.Single().Should().BeEquivalentTo(expectedLogEvent);
                }
        }
Example #3
0
        public void Log_SimpleMessage_LogsValidEvent()
        {
            var expectedLogEvent = new MicrosoftMemoryLoggerEvent
            {
                Category = TestCategoryName,
                LogLevel = MsLogLevel.Warning,
                Message  = "message",
                State    = new ImmutableArrayDictionary <string, object>(
                    new Dictionary <string, object>
                {
                    [MicrosoftLogProperties.OriginalFormatKey] = "message"
                },
                    StringComparer.Ordinal)
            };

            microsoftLog.Warn("message");

            microsoftLogger.Events.Single().Should().BeEquivalentTo(expectedLogEvent);
        }
Example #4
0
        public void Log_MessageWithPositionPlaceholders_LogsValidEvent()
        {
            var expectedLogEvent = new MicrosoftMemoryLoggerEvent
            {
                Category = TestCategoryName,
                LogLevel = MsLogLevel.Critical,
                Message  = "message v1 v2",
                State    = new ImmutableArrayDictionary <string, object>(
                    new Dictionary <string, object>
                {
                    [MicrosoftLogProperties.OriginalFormatKey] = "message {0} {1}",
                    ["0"] = "v1",
                    ["1"] = "v2"
                },
                    StringComparer.Ordinal)
            };

            microsoftLog.Fatal("message {0} {1}", "v1", "v2");

            microsoftLogger.Events.Single().Should().BeEquivalentTo(expectedLogEvent);
        }
Example #5
0
        public void Log_SimpleMessageWithSimpleContext_LogsValidEvent()
        {
            const string context          = "ctx1";
            var          expectedLogEvent = new MicrosoftMemoryLoggerEvent
            {
                Category = TestCategoryName,
                LogLevel = MsLogLevel.Warning,
                Message  = "message",
                State    = new ImmutableArrayDictionary <string, object>(
                    new Dictionary <string, object>
                {
                    [MicrosoftLogProperties.OriginalFormatKey] = "message",
                    ["sourceContext"] = new SourceContextValue(context)
                },
                    StringComparer.Ordinal)
            };

            microsoftLog.ForContext(context).Warn("message");

            microsoftLogger.Events.Single().Should().BeEquivalentTo(expectedLogEvent);
        }