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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
public void InvalidLayoutWillParsePartly() { SimpleLayout l = @"aaa ${iDontExist} bbb"; var le = LogEventInfo.Create(LogLevel.Info, "logger", "message"); Assert.Equal("aaa bbb", l.Render(le)); }
public void Test1() { Layout layout = "${event-properties:prop1}"; LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "prop1", "bbb"); // empty Assert.Equal("", layout.Render(logEvent)); }
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)); }
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)); }
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)); }
public void WrapLineAtPositionAtExactTextLengthTest() { SimpleLayout l = "${message:wrapline=6}"; var le = LogEventInfo.Create(LogLevel.Info, "logger", "foobar"); Assert.Equal("foobar", l.Render(le)); }
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); }
public void NoSet() { Layout layout = "${event-properties:prop1}"; LogEventInfo logEvent = LogEventInfo.Create(LogLevel.Info, "logger1", "message1"); // empty Assert.Equal("", layout.Render(logEvent)); }
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); }
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)); }
public void NegativeWhenTest() { SimpleLayout l = @"${message:when=logger=='logger'}"; var le = LogEventInfo.Create(LogLevel.Info, "logger2", "message"); Assert.AreEqual("", l.Render(le)); }
public void Test1() { Layout l = "${event-context:aaa}"; LogEventInfo lei = LogEventInfo.Create(LogLevel.Info, "aaa", "bbb"); // empty Assert.AreEqual("", l.Render(lei)); }
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)); }
public void WrapLineAtNegativePositionTest() { SimpleLayout l = "${message:wrapline=0}"; var le = LogEventInfo.Create(LogLevel.Info, "logger", "foobar"); Assert.Equal("foobar", l.Render(le)); }
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))); } }
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); } }
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)); } }
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)); }
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)); }
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)); }
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); }
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); }
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)); }
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)); }