Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aspnet-request vars are empty in ASPNET Core 3.1 API #584

Closed
jchapmantcg opened this issue Jun 21, 2020 · 4 comments
Closed

aspnet-request vars are empty in ASPNET Core 3.1 API #584

jchapmantcg opened this issue Jun 21, 2020 · 4 comments
Labels

Comments

@jchapmantcg
Copy link

jchapmantcg commented Jun 21, 2020

Type: Question/Bug

  • All other vars like ${message} work, only the aspnet-request ones do not.

NLog version: 4.7.2

NLog.Web.AspNetCore version: 4.9.2

NLog.Extensions.Logging version: N/A

Platform: .NET Core 3.1

Current NLog config (xml or C#, if relevant)


// Console
config.AddTarget(new ConsoleTarget("logconsole"));

// Papertrail
var paperTrail = new SyslogTarget();
paperTrail.Name = "PaperTrail";
paperTrail.MessageCreation.Facility = NLog.Targets.Syslog.Settings.Facility.Local7;
paperTrail.MessageCreation.Rfc5424.AppName = "TESTAPP";
paperTrail.MessageSend.Protocol = NLog.Targets.Syslog.Settings.ProtocolType.Tcp;
paperTrail.MessageSend.Tcp.Server = "xxxxxxx";
paperTrail.MessageSend.Tcp.Port = xxxxxx;
paperTrail.MessageSend.Tcp.Tls = new NLog.Targets.Syslog.Settings.TlsConfig { Enabled = true };
paperTrail.Layout = "${uppercase:${level}}|${aspnet-Request-Method}|${aspnet-request-url:IncludeQueryString=true}|${message}|${exception:format=tostring}";
config.AddTarget(paperTrail);

config.AddRuleForAllLevels("PaperTrail");


_logger = NLogBuilder.ConfigureNLog(config).GetCurrentClassLogger();

In case of a BUG:

  • What is the current result?
    All ${aspnet-request-xxxx} vars are empty

  • What is the expected result?
    Values to be valid

  • Did you checked the Internal log?
    Yes. It did not show anything related.

  • Are there any workarounds? yes/no
    Not that I'm aware of.

  • Is there a version in which it did work?
    No.

  • Can you help us by writing an unit test?
    Yes.

@snakefoot
Copy link
Contributor

snakefoot commented Jun 21, 2020

When #540 is merged and released, then things will get easier.

But while waiting for that, then I guess the following clumsy work-around can be used:

/* Register NLog.Web Extensions */
var dummy = LogginConfiguration();
NLogBuilder.ConfigureNLog(dummy);

/* Setup the actual confg, now has access to NLog.Web Extensions */

_logger = NLogBuilder.ConfigureNLog(config).GetCurrentClassLogger();

@jchapmantcg
Copy link
Author

@snakefoot This produced the same results.

@snakefoot
Copy link
Contributor

snakefoot commented Jun 21, 2020

@jchapmantcg The ${aspnet-Request} requires two things:

You can setup the service-locator using AddNLog or UseNLog.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder.UseStartup<Startup>();
      })
      .ConfigureLogging(logging =>
      {
          logging.ClearProviders();
          logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
      })
      .UseNLog();  // NLog: Setup NLog for Dependency injection (Make ${aspnet-Request} work)

See also: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

@jchapmantcg
Copy link
Author

This is what I was missing! Thanks @snakefoot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants