簡體   English   中英

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"                                                         |
'-------------------------------------------------------------------------------------------------------------'-----------------------------------------------------------------------------------------------------------------------'

天啊! - 不會像預期的那樣呈現,只會呈現第一個數組元素。 有關詳細信息請參閱Format(String, Object[])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM