I was reading whole topics on StackOverflow and NLog's wiki but nothing works for me..
Maybe I am unable to see something and someone could see it.
I have a minimal API and I would like to log my request from a certain logger.
I found that from v5.1.0 of NLog.Web.AspNetCore
this is possible via the ${aspnet-request-posted-body}
property, but I am unable to make it work.
In my first try, Program.cs
looked like this:
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("Init main...");
// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();
// and this is what I found
// from https://github.com/NLog/NLog/wiki/AspNet-Request-posted-body-layout-renderer
app.UseMiddleware<NLogRequestPostedBodyMiddleware>();
and I have an appsettings.json
file like below:
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "Info",
"internalLogFile": "${basedir}/internal-nlog.txt",
"targets": {
"async": true,
"reqlogfile": {
"type": "File",
"fileName": "${basedir}/logs/${shortdate}.txt",
"layout": "${longdate} ${uppercase:${level}} | ${logger} | ${message} | Payload: ${aspnet-request-posted-body}"
},
"reqlogconsole": {
"type": "Console",
"layout": "${longdate} ${uppercase:${level}} | ${logger} | ${message} | Payload: ${aspnet-request-posted-body}"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Info",
"writeTo": "reqlogfile"
},
{
"logger": "*",
"minLevel": "Info",
"writeTo": "reqlogconsole"
}
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
but Payload was empty always.. Anyone knows how to connect the appsettings
definiton with usage of log request body?
It looks like it is really simple but I am out of ideas.
I simply doesn't remember how many times I tried and what have I done, one thing is constant: I am unable to make it work.
I am also wondering if someone was able to get rid of Microsoft hosting logs.. this also is not listening to me (many config possibilities tried - nothing worked :| ).
Any help will be much appreciate!