I am using ${aspnet-request-posted-body} in my nlog.config file ,I have also added configuartions in startup file. but ${aspnet-request-posted-body} not logging request body and ${mdlc:item=RequestId} also not working
<target xsi:type="File" name="allfile" fileName="c:LogsApi_.log"
layout="==>[${event-properties:item=traceLevel}: User=${aspnet-user-identity}; ]
${when:when=('${event-properties:item=traceLevel}' == 'Verbose' and
'${aspnet-request-method}' != 'GET') > 0 and length('${mdlc:item=RequestId}') > 0:inner=${newline}Request-Body: ${aspnet-request-posted-body}}
${newline}Logger name: ${logger}
${newline}" />
${aspnet-request-posted-body}and${mdlc:item=RequestId}used in nlog file- To enable
${aspnet-request-posted-body}in startup I used this code.
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next?.Invoke();
});
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Would probably start with something simple like this:
<target xsi:type="File" name="allfile" fileName="c:LogsApi_.log"
layout="RequestId=${mdlc:item=RequestId} RequestBody=${aspnet-request-posted-body}" />
And then enable the NLog InternalLogger at Debug-Level and look for clues:
- https://github.com/NLog/NLog/wiki/Internal-Logging
- https://github.com/NLog/NLog/wiki/Logging-troubleshooting
Besides calling EnableBuffering() then you also need to correctly register NLog as LoggingProvider with the method UseNLog(). See also https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3
If you are not using Microsoft ILogger for logging within the context for the active HttpRequest, then RequestId will not be available.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0