Search code examples
loggingnlogazure-table-storagestring-interpolation

LoggerMessage string interpolation is not working - NLOG & Azure table storage


I am using Nlog & Azure table storage, and my log write (LoggerExtensions.cs) looks like below.

TestMessageAction = LoggerMessage.Define<string>(
            LogLevel.Error,
            new EventId(1000, nameof(TestMessage)),
            "Test Error Message (Key = {key})");

public static void TestMessage(this ILogger logger, string key, Exception ex)
    {
        TestMessageAction(logger, key, ex);
    }

This is working fine if the target is file or Database. Not working fine in azure table storage.

Example :

for the same code above , I am seeing below in file

Test Error Message (Key = ef5bbee2-ac68-4dee-8497-54d5d9691a92)

but in Azure table storage

Test Error Message (Key = key)

and Nlog target for Azure table storage : Nlog.config

<target xsi:type="AzureTableStorage"
        name="NLogAzureTable"
        layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
        connectionString="******"
        tableName="ServiceLogs"
        logTimeStampFormat="O" />

 <rules>
<logger name="*" minlevel="Info" writeTo="NLogAzureTable"></logger>
 </rules>

Azure logs looks ok if I dont use LoggerMessage.

_logger.LogInfo($"Test Info : {key}");

writes
Test Info : ef5bbee2-ac68-4dee-8497-54d5d9691a92

Please let me know what I am missing here.

Thank you.


Solution

  • Not sure if it is a bug but you can see here:

    https://github.com/JDetmar/NLog.Extensions.AzureStorage/blob/6ed03bfa183b231dffeb3733b912c87fe8efc6f6/src/NLog.Extensions.AzureStorage/NLogEntity.cs#L24

    Then it does this:

    Message = logEvent.Message;
    

    Instead of this:

    Message = logEvent.FormattedMessage;
    

    You should see that FullMessage contains the correct formatting.

    Maybe create an issue and ask the maintainer of the project: https://github.com/JDetmar/NLog.Extensions.AzureStorage/issues