Search code examples
c#.netserilog

Serilog templates and Microsoft.Extensions.Logging


Do Serilog templates work when using ILogger from the Microsoft.Extensions.Logging package?

using Microsoft.Extensions.Logging;
// ...
ILogger logger {get; set;}
// ...
logger.Log("Entering {@args}", args);

Solution

  • Confirming the example in the question works.

    https://github.com/akovac35/Logging#message-template-examples

    Rendering examples for Serilog are provided below:

    .-------------------------------------------------------------------------------------------------------------.-----------------------------------------------------------------------------------------------------------------------.
    |                                              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! - will not render as perhaps expected, only the first array element will be rendered. See Format(String, Object[]) for more information.