简体   繁体   中英

Serilog Expressions output template newline in Azure Function app

I have just added the Serilog Expressions package to my Azure Function App, so that I could use the ability to shorten the SourceContext down to just the class name (which works beautifully btw). The function app has its config parameters stored in the Azure portal in the function app's Settings -> Configuration -> Application Settings section. The setting for the log entry template is named SerilogSettings:OutputTemplate, and the value entered for that setting is exactly the same there as it is in a different app where it works correctly:

{@t:yyyy-MM-dd HH:mm:ss.fff zzz}|{CorrelationId}|{@l:u3}|{Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}|{@m}\n{@x}

The problem I'm seeing is with the newline character in there. The Application Settings section in Azure has an "Advanced Edit" view, where you can see that the setting values entered are actually translated behind the scenes into a big json string, and that json string is what actually gets read by the application at startup time. Here's a key chunk from that json string:

[
  ...
  {
    "name": "SerilogSettings:OutputTemplate",
    "value": "{@t:yyyy-MM-dd HH:mm:ss.fff zzz}|{CorrelationId}|{@l:u3}|{Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}|{@m}\\n{@x}",
    "slotSetting": false
  },
  ...
]  

Notice that the newline \n has been escaped, and is now \\n . So now at startup time, that template string is sent into Serilog, and it does not understand the \\n . The end result is that the log entries written do not have any newline characters in them, and the log file consists of one enormously long line. What are my options to address this while still using the Expressions package?

What are my options to address this while still using the Expressions package?

When using Serilog with the message property in the template should have the format specifier 1 to achieve the desired format:

"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:l}{NewLine}{Exception}"

refer this blog for more details of expression implementations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM