Exemplo n.º 1
0
        public void InnerLayoutWithHashTest_and_layoutrender()
        {
            SimpleLayout l = @"${when:when=1 == 1:inner=${counter}/Log_{#\}.log}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("1/Log_{#}.log", l.Render(le));
        }
Exemplo n.º 2
0
        public void InnerLayoutWithHashTest_need_escape()
        {
            SimpleLayout l = @"${when:when=1 == 1:inner=L\}.log}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("L}.log", l.Render(le));
        }
Exemplo n.º 3
0
        public void InnerLayoutWithBracketsTest_needEscape5()
        {
            SimpleLayout l = @"${when:when=1 == 1:inner=\}{a\}.log}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("}{a}.log", l.Render(le));
        }
Exemplo n.º 4
0
        public void InnerLayoutWithBracketsTest_no_escape()
        {
            SimpleLayout l = @"${when:when=1 == 1:Inner=Test{Hello}}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("Test{Hello}", l.Render(le));
        }
Exemplo n.º 5
0
        public void InnerLayoutWithSlashTest()
        {
            SimpleLayout l = @"${when:when=1 == 1:Inner=Test\Hello}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("Test\\Hello", l.Render(le));
        }
Exemplo n.º 6
0
        public void InnerLayoutWithColonTest_with_workaround()
        {
            SimpleLayout l = @"${when:when=1 == 1:Inner=Test${literal:text=\:} Hello}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("Test: Hello", l.Render(le));
        }
Exemplo n.º 7
0
        public void InvalidLayoutWillParsePartly()
        {
            SimpleLayout l = @"aaa ${iDontExist} bbb";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "message");

            Assert.Equal("aaa  bbb", l.Render(le));
        }
Exemplo n.º 8
0
        public void Test1()
        {
            Layout       layout   = "${event-properties:prop1}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "prop1", "bbb");

            // empty
            Assert.Equal("", layout.Render(logEvent));
        }
Exemplo n.º 9
0
 private LogEventInfo CreateLogEventInfo(LogLevel nLogLogLevel, string message, IReadOnlyList <KeyValuePair <string, object> > parameterList)
 {
     if (parameterList != null && parameterList.Count > 1 && IsNonDigitValue(parameterList[0].Key))
     {
         return(CreateLogEventInfoWithMultipleParameters(nLogLogLevel, message, parameterList));
     }
     return(LogEventInfo.Create(nLogLogLevel, _logger.Name, message));
 }
Exemplo n.º 10
0
        public void WrapLineAtPositionOnceTextLengthNotMultipleTest()
        {
            SimpleLayout l = "${message:wrapline=3}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "fooba");

            Assert.Equal("foo" + System.Environment.NewLine + "ba", l.Render(le));
        }
Exemplo n.º 11
0
        public void WrapLineMultipleTimesTest()
        {
            SimpleLayout l = "${message:wrapline=3}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "foobarbaz");

            Assert.Equal("foo" + System.Environment.NewLine + "bar" + System.Environment.NewLine + "baz", l.Render(le));
        }
Exemplo n.º 12
0
        public void WrapLineAtPositionAtExactTextLengthTest()
        {
            SimpleLayout l = "${message:wrapline=6}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "foobar");

            Assert.Equal("foobar", l.Render(le));
        }
Exemplo n.º 13
0
        void FuncLayoutRendererRegisterTest2()
        {
            LayoutRenderer.Register("message-length", (info) => info.Message.Length);
            Layout l      = "${message-length}";
            var    result = l.Render(LogEventInfo.Create(LogLevel.Error, "logger-adhoc", "1234567890"));

            Assert.Equal("10", result);
        }
Exemplo n.º 14
0
        public void NoSet()
        {
            Layout       layout   = "${event-properties:prop1}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1");

            // empty
            Assert.Equal("", layout.Render(logEvent));
        }
Exemplo n.º 15
0
        protected void AssertLayoutRendererOutput(Layout l, string expected)
        {
            l.Initialize(null);
            string actual = l.Render(LogEventInfo.Create(LogLevel.Info, "loggername", "message"));

            l.Close();
            Assert.Equal(expected, actual);
        }
Exemplo n.º 16
0
        public void JsonFormat()
        {
            Layout       layout   = "${event-properties:prop1:format=@}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1");

            logEvent.Properties["prop1"] = new string[] { "Hello", "World" };
            Assert.Equal("[\"Hello\",\"World\"]", layout.Render(logEvent));
        }
Exemplo n.º 17
0
        public void NegativeWhenTest()
        {
            SimpleLayout l = @"${message:when=logger=='logger'}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger2", "message");

            Assert.AreEqual("", l.Render(le));
        }
Exemplo n.º 18
0
        public void Test1()
        {
            Layout       l   = "${event-context:aaa}";
            LogEventInfo lei = LogEventInfo.Create(LogLevel.Info, "aaa", "bbb");

            // empty
            Assert.AreEqual("", l.Render(lei));
        }
Exemplo n.º 19
0
        public void Test4()
        {
            Layout       l   = "${rot13:${event-context:aaa}}";
            LogEventInfo lei = LogEventInfo.Create(LogLevel.Info, "aaa", "bbb");

            lei.Properties["aaa"] = "HELLO";
            Assert.AreEqual("URYYB", l.Render(lei));
        }
Exemplo n.º 20
0
        public void WrapLineAtNegativePositionTest()
        {
            SimpleLayout l = "${message:wrapline=0}";

            var le = LogEventInfo.Create(LogLevel.Info, "logger", "foobar");

            Assert.Equal("foobar", l.Render(le));
        }
Exemplo n.º 21
0
 private void Log(LogLevel logLevel, Exception exception, string message, object[] objs)
 {
     // Skip RenderString if no one is listening
     if (_logger.IsEnabled(logLevel))
     {
         // Allow NLog callsite to work, by providing typeof(NLogLogger)
         _logger.Log(typeof(NLogLogger), LogEventInfo.Create(logLevel, _logger.Name, exception, null, _loggerFactory.RenderString(message, objs)));
     }
 }
Exemplo n.º 22
0
 public static void LogErrorSimple(Exception ex)
 {
     if (!(ex is BusinessException))
     {
         var info = LogEventInfo.Create(LogLevel.Error, "", ex.Message);
         info.Properties["exception"] = ex.ToString();
         _applicationlogger.Log(info);
     }
 }
Exemplo n.º 23
0
            public void Log(MySqlConnectorLogLevel level, string message, object?[]?args = null, Exception?exception = null)
            {
                LogLevel logLevel = GetLevel(level);

                if (m_logger.IsEnabled(logLevel))
                {
                    m_logger.Log(s_loggerType, LogEventInfo.Create(logLevel, m_logger.Name, exception, CultureInfo.InvariantCulture, message, args));
                }
            }
Exemplo n.º 24
0
        private LogEventInfo CreateLogEventInfo(LogLevel nLogLogLevel, string message, IReadOnlyList <KeyValuePair <string, object> > parameterList)
        {
            if (parameterList != null && parameterList.Count > 1)
            {
                // More than a single parameter (last parameter is the {OriginalFormat})
                var firstParameterName = parameterList[0].Key;
                if (!string.IsNullOrEmpty(firstParameterName))
                {
                    if (firstParameterName.Length != 1 || !char.IsDigit(firstParameterName[0]))
                    {
#if NETSTANDARD2_0
                        var    originalFormat  = parameterList[parameterList.Count - 1];
                        string originalMessage = null;
                        if (originalFormat.Key == OriginalFormatPropertyName)
                        {
                            // Attempt to capture original message with placeholders
                            originalMessage = originalFormat.Value as string;
                        }

                        var messageTemplateParameters = new NLogMessageParameterList(parameterList, originalMessage != null);
                        var eventInfo = new LogEventInfo(nLogLogLevel, _logger.Name, originalMessage ?? message, messageTemplateParameters);
                        if (originalMessage != null)
                        {
                            eventInfo.Parameters = new object[messageTemplateParameters.Count + 1];
                            for (int i = 0; i < messageTemplateParameters.Count; ++i)
                            {
                                eventInfo.Parameters[i] = messageTemplateParameters[i].Value;
                            }
                            eventInfo.Parameters[messageTemplateParameters.Count] = message;
                            eventInfo.MessageFormatter = (l) => (string)l.Parameters[l.Parameters.Length - 1];
                        }
                        return(eventInfo);
#else
                        var eventInfo = LogEventInfo.Create(nLogLogLevel, _logger.Name, message);
                        for (int i = 0; i < parameterList.Count; ++i)
                        {
                            var parameter = parameterList[i];
                            if (string.IsNullOrEmpty(parameter.Key))
                            {
                                break;  // Skip capture of invalid parameters
                            }
                            var parameterName = parameter.Key;
                            switch (parameterName[0])
                            {
                            case '@': parameterName = parameterName.Substring(1); break;

                            case '$': parameterName = parameterName.Substring(1); break;
                            }
                            eventInfo.Properties[parameterName] = parameter.Value;
                        }
                        return(eventInfo);
#endif
                    }
                }
            }
            return(LogEventInfo.Create(nLogLogLevel, _logger.Name, message));
        }
Exemplo n.º 25
0
        public void DateTimeFormat()
        {
            Layout       layout   = "${event-properties:prop1:format=yyyy-M-dd}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1");

            logEvent.Properties["prop1"] = new DateTime(2020, 2, 21, 23, 1, 0);

            Assert.Equal("2020-2-21", layout.Render(logEvent));
        }
Exemplo n.º 26
0
        public void DateTime()
        {
            Layout       layout   = "${event-properties:prop1}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1");

            logEvent.Properties["prop1"] = new DateTime(2020, 2, 21, 23, 1, 0);

            Assert.Equal("02/21/2020 23:01:00", layout.Render(logEvent));
        }
Exemplo n.º 27
0
        private LogEventInfo Log(string loggerName, LogLevel level, Exception exception, IFormatProvider formatProvider, string message)
        {
            ILogger logger       = LogManager.GetLogger(loggerName);
            var     logEventInfo = LogEventInfo.Create(level, logger.Name, exception, formatProvider, message);

            logger.Log(logEventInfo);

            return(logEventInfo);
        }
Exemplo n.º 28
0
        public void LayoutWithListParamTest(string input, string propname, string expected)
        {
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("layoutrenderer-with-list", typeof(LayoutRendererWithListParam));
            SimpleLayout l = $@"${{layoutrenderer-with-list:{propname}={input}}}";

            var le     = LogEventInfo.Create(LogLevel.Info, "logger", "message");
            var actual = l.Render(le);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 29
0
        public void Test2()
        {
            Layout       layout   = "${event-properties:prop1}";
            LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1");

            logEvent.Properties["prop1"] = "bbb";

            // empty
            Assert.Equal("bbb", layout.Render(logEvent));
        }
Exemplo n.º 30
0
        public void Test2()
        {
            Layout       l   = "${event-context:aaa}";
            LogEventInfo lei = LogEventInfo.Create(LogLevel.Info, "aaa", "bbb");

            lei.Properties["aaa"] = "bbb";

            // empty
            Assert.Equal("bbb", l.Render(lei));
        }