0

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!

user7247147
  • 1,045
  • 1
  • 10
  • 24
frozzen10
  • 101
  • 4
  • Try changing `"internalLogLevel": "Info"` to `"internalLogLevel": "Debug"`, and check the output in `"internalLogFile"` for possible issues. – Rolf Kristensen May 15 '23 at 19:41
  • it says `2023-05-16 08:56:24.2271 Debug No available HttpContext, because outside valid request context. Logger: Microsoft.Hosting.Lifetime` not once but for my Program class too. Do you know what does it mean? – frozzen10 May 16 '23 at 07:07
  • `No available HttpContext, because outside valid request context` means that your NLog Target Layout is triggered by logging-statement not related to ongoing HttpRequest. Could you add [${aspnet-request-contentlength}](https://github.com/NLog/NLog/wiki/AspNet-Response-ContentLength-Layout-Renderer) and [${aspnet-request-has-posted-body}](https://github.com/NLog/NLog/wiki/AspNet-Request-Has-Posted-Body-Layout-Renderer) to the Layout just to verify that your active HttpRequest actually includes posted-body-payload ? – Rolf Kristensen May 16 '23 at 16:23

0 Answers0