Serilog 模板和 Microsoft.Extensions.Logging
Serilog templates and Microsoft.Extensions.Logging
使用 Microsoft.Extensions.Logging 包中的 ILogger 时,Serilog 模板是否有效?
using Microsoft.Extensions.Logging;
// ...
ILogger logger {get; set;}
// ...
logger.Log("Entering {@args}", args);
确认问题中的示例有效。
https://github.com/akovac35/Logging#message-template-examples
下面提供了 Serilog 的渲染示例:
.-------------------------------------------------------------------------------------------------------------.-----------------------------------------------------------------------------------------------------------------------.
| Logger invocation | Rendered message |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(LogLeveILogger.Information, 1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(1, typeof(int), 3) | Entering: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(LogLeveILogger.Information, 1, typeof(int), 3) | Entering: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering() | Entering |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(LogLeveILogger.Information, 1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(1, typeof(int), 3) | Entering: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(LogLeveILogger.Information, 1, typeof(int), 3) | Entering: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(LogLeveILogger.Information, 1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(1, typeof(int), 3) | Exiting: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(LogLeveILogger.Information, 1, typeof(int), 3) | Exiting: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting() | Exiting |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(LogLeveILogger.Information, 1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(1, typeof(int), 3) | Exiting: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(LogLeveILogger.Information, 1, typeof(int), 3) | Exiting: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}, {@1}", new TestType(), "next arg") | Testing: TestType { TestString: "xyz", TestNumber: "123" }, "next arg" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}, {1}", new TestType(), "next arg") | Testing: "com.github.akovac35.Logging.Serilog.Tests.TestType", "next arg" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}", new { Amount = 108, Message = "Hello" }) | Testing: { Amount: 108, Message: "Hello" } |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}", new { Amount = 108, Message = "Hello" }) | Testing: "{ Amount = 108, Message = Hello }" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}", new object[] { new TestType[] { new TestType(), new TestType() } }) | Testing: [TestType { TestString: "xyz", TestNumber: "123" }, TestType { TestString: "xyz", TestNumber: "123" }] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}", new object[] { new TestType[] { new TestType(), new TestType() } }) | Testing: ["com.github.akovac35.Logging.Serilog.Tests.TestType", "com.github.akovac35.Logging.Serilog.Tests.TestType"] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ATN! ILogger.LogInformation("Testing: {@0}", new TestType[] { new TestType(), new TestType() }) | Testing: TestType { TestString: "xyz", TestNumber: "123" } |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ATN! ILogger.LogInformation("Testing: {0}", new TestType[] { new TestType(), new TestType() }) | Testing: "com.github.akovac35.Logging.Serilog.Tests.TestType" |
'-------------------------------------------------------------------------------------------------------------'-----------------------------------------------------------------------------------------------------------------------'
ATN! - 不会像预期的那样渲染,只会渲染第一个数组元素。有关详细信息,请参阅 Format(String, Object[])。
使用 Microsoft.Extensions.Logging 包中的 ILogger 时,Serilog 模板是否有效?
using Microsoft.Extensions.Logging;
// ...
ILogger logger {get; set;}
// ...
logger.Log("Entering {@args}", args);
确认问题中的示例有效。
https://github.com/akovac35/Logging#message-template-examples
下面提供了 Serilog 的渲染示例:
.-------------------------------------------------------------------------------------------------------------.-----------------------------------------------------------------------------------------------------------------------.
| Logger invocation | Rendered message |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(LogLeveILogger.Information, 1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(1, typeof(int), 3) | Entering: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering(LogLeveILogger.Information, 1, typeof(int), 3) | Entering: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Entering() | Entering |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(LogLeveILogger.Information, 1, 2, 3) | Entering: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(1, typeof(int), 3) | Entering: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.EnteringSimpleFormat(LogLeveILogger.Information, 1, typeof(int), 3) | Entering: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(LogLeveILogger.Information, 1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(1, typeof(int), 3) | Exiting: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting(LogLeveILogger.Information, 1, typeof(int), 3) | Exiting: [1, System.Int32, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.Exiting() | Exiting |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(LogLeveILogger.Information, 1, 2, 3) | Exiting: [1, 2, 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(1, typeof(int), 3) | Exiting: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.ExitingSimpleFormat(LogLeveILogger.Information, 1, typeof(int), 3) | Exiting: [1, "System.Int32", 3] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}, {@1}", new TestType(), "next arg") | Testing: TestType { TestString: "xyz", TestNumber: "123" }, "next arg" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}, {1}", new TestType(), "next arg") | Testing: "com.github.akovac35.Logging.Serilog.Tests.TestType", "next arg" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}", new { Amount = 108, Message = "Hello" }) | Testing: { Amount: 108, Message: "Hello" } |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}", new { Amount = 108, Message = "Hello" }) | Testing: "{ Amount = 108, Message = Hello }" |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {@0}", new object[] { new TestType[] { new TestType(), new TestType() } }) | Testing: [TestType { TestString: "xyz", TestNumber: "123" }, TestType { TestString: "xyz", TestNumber: "123" }] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ILogger.LogInformation("Testing: {0}", new object[] { new TestType[] { new TestType(), new TestType() } }) | Testing: ["com.github.akovac35.Logging.Serilog.Tests.TestType", "com.github.akovac35.Logging.Serilog.Tests.TestType"] |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ATN! ILogger.LogInformation("Testing: {@0}", new TestType[] { new TestType(), new TestType() }) | Testing: TestType { TestString: "xyz", TestNumber: "123" } |
:-------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------:
| ATN! ILogger.LogInformation("Testing: {0}", new TestType[] { new TestType(), new TestType() }) | Testing: "com.github.akovac35.Logging.Serilog.Tests.TestType" |
'-------------------------------------------------------------------------------------------------------------'-----------------------------------------------------------------------------------------------------------------------'
ATN! - 不会像预期的那样渲染,只会渲染第一个数组元素。有关详细信息,请参阅 Format(String, Object[])。