[英]NLog custom LayoutRenderer is rendering log events as empty string
[英]NLog LogEventInfo.Message is {0} instead of null or empty on custom LayoutRenderer
我有一個自定義的LayoutRenderer
。
如果我調用以下語句,那么我總是得到LogEventInfo.Message = {0}
而不是null
或""
。 我是不是誤會了什么?
try
{
int a = 1;
int b = 0;
var c = a / b;
}
catch (Exception e)
{
var logger = LogManager.GetCurrentClassLogger();
logger.Error(e);
}
public class HtLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
// logEvent.Message is {0} instead of being null or empty ""
}
}
我只發布了一個很小的例子。
我有兩個自定義Exceptions
: HtReturnStatus
和HtExceptions
。 它們都是Exception
類型。 HtReturnStatus
可以包含HtExceptions
。 HtLayoutRenderer
正在解包對象並制作方便的日志布局。 提供什么類型的類/對象並不重要。
我的問題是{0}
。 如果這是正常行為,那么我會檢查logEvent.Message
是否為{0}
並忽略它。
try
{
int a = 1;
int b = 0;
var c = a / b;
}
catch (Exception e)
{
LayoutRenderer.Register<HtLayoutRenderer>("htmessage");
TraceTarget target = new TraceTarget();
target.Name = "console";
target.Layout = Layout.FromString("${longdate} | ${level:uppercase=true} | ${logger} | ${htmessage}");
LogManager.Configuration = new LoggingConfiguration();
LogManager.Configuration.AddTarget(target);
LogManager.Configuration.LoggingRules.Insert(0, new LoggingRule("*", LogLevel.FromString("Trace"), target));
LogManager.ReconfigExistingLoggers();
var logger = LogManager.GetCurrentClassLogger();
logger.Error("1");
logger.Error(e);
logger.Error("2");
logger.Error(e, "Hello World");
logger.Error("");
logger.Error("3");
logger.Error(HtReturnStatus.Failed(e), "Lorem Ipsum");
logger.Error("");
logger.Error("4");
logger.Error(HtReturnStatus.Failed(e, "Error on division"));
logger.Error("");
logger.Error("5");
logger.Error(HtReturnStatus.Failed(HtErrorCode.OPC_BAD_CERTIFICATE_INVALID));
logger.Error("");
logger.Error("6");
logger.Error(HtReturnStatus.Failed(HtErrorCode.OPC_BAD_UNEXPECTED_ERROR, "Error on division #2"));
logger.Error("");
logger.Error("7");
logger.Error(new HtException(HtErrorCode.CODE_READER_DMC_MultipleMatches));
logger.Error("");
logger.Error("8");
logger.Error(new HtException(HtErrorCode.CODE_READER_DMC_MultipleMatches, "hello world"));
logger.Error("");
logger.Error("9");
logger.Error(new HtException(HtErrorCode.CODE_READER_DMC_MultipleMatches, "lorem ipsum", e));
}
testhost Error: 0 : 2020-01-21 10:14:01.1168 | ERROR | Haprotec.Error.NLogOutputTest | 1
testhost Error: 0 : 2020-01-21 10:14:01.1383 | ERROR | Haprotec.Error.NLogOutputTest | {0} - Attempted to divide by zero. - System.DivideByZeroException: Attempted to divide by zero.
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.1572 | ERROR | Haprotec.Error.NLogOutputTest | 2
testhost Error: 0 : 2020-01-21 10:14:01.1572 | ERROR | Haprotec.Error.NLogOutputTest | Hello World - Attempted to divide by zero. - System.DivideByZeroException: Attempted to divide by zero.
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.1572 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.1572 | ERROR | Haprotec.Error.NLogOutputTest | 3
testhost Error: 0 : 2020-01-21 10:14:01.1742 | ERROR | Haprotec.Error.NLogOutputTest | Lorem Ipsum - System.DivideByZeroException: Attempted to divide by zero.
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.1742 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.1742 | ERROR | Haprotec.Error.NLogOutputTest | 4
testhost Error: 0 : 2020-01-21 10:14:01.1742 | ERROR | Haprotec.Error.NLogOutputTest | {0} - Error on division - System.DivideByZeroException: Attempted to divide by zero.
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.1905 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.1905 | ERROR | Haprotec.Error.NLogOutputTest | 5
testhost Error: 0 : 2020-01-21 10:14:01.2166 | ERROR | Haprotec.Error.NLogOutputTest | {0} - Haprotec.Error.HtException: 563021 (OPC_BAD_CERTIFICATE_INVALID)
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.2204 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.2204 | ERROR | Haprotec.Error.NLogOutputTest | 6
testhost Error: 0 : 2020-01-21 10:14:01.2204 | ERROR | Haprotec.Error.NLogOutputTest | {0} - Error on division #2 - Haprotec.Error.HtException: 563001 (OPC_BAD_UNEXPECTED_ERROR)
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.2204 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.2204 | ERROR | Haprotec.Error.NLogOutputTest | 7
testhost Error: 0 : 2020-01-21 10:14:01.2374 | ERROR | Haprotec.Error.NLogOutputTest | {0} - Haprotec.Error.HtException: 500251 (CODE_READER_DMC_MultipleMatches)
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.2374 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.2374 | ERROR | Haprotec.Error.NLogOutputTest | 8
testhost Error: 0 : 2020-01-21 10:14:01.2374 | ERROR | Haprotec.Error.NLogOutputTest | {0} - hello world - Haprotec.Error.HtException: 500251 (CODE_READER_DMC_MultipleMatches)
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
testhost Error: 0 : 2020-01-21 10:14:01.2374 | ERROR | Haprotec.Error.NLogOutputTest |
testhost Error: 0 : 2020-01-21 10:14:01.2506 | ERROR | Haprotec.Error.NLogOutputTest | 9
testhost Error: 0 : 2020-01-21 10:14:01.2506 | ERROR | Haprotec.Error.NLogOutputTest | {0} - lorem ipsum - System.DivideByZeroException: Attempted to divide by zero.
StackTrace: at Haprotec.Error.NLogOutputTest.HtReturnStatusOutputTest() in D:\Repositories\gitlab.haprotec\dotNet\HtCore\src\HtCore.Tests\Error\NLogOutputTest.cs:line 26
這完全是故意的。 您正在調用此記錄器方法,其中一個記錄隨機對象:
Logger.Error<T>(T value);
這是對此的簡寫,以確保string.Format
可以工作:
Logger.Error("{0}", value);
但是您可能已經注意到,已經為Exception
實現了特殊處理。 NLog 執行額外檢查以捕獲${exception}
的異常參數,但不會更改格式字符串。
在您的HtLayoutRenderer
您可以像這樣識別它:
public class HtLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
if ( logevent.Exception != null
&& logevent.Message == "{0}"
&& logevent.Parameters?.Length == 1
&& ReferenceEquals(logevent.Exception, logevent.Parameters[0])
{
// Exception given as single parameter without any message
}
}
}
請注意, LogEventInfo.Message
和LogEventInfo.FormattedMessage
之間存在差異
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.