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

Cant get Seq target to work with $configsetting #46

Closed
SirGordon opened this issue Nov 29, 2019 · 8 comments
Closed

Cant get Seq target to work with $configsetting #46

SirGordon opened this issue Nov 29, 2019 · 8 comments

Comments

@SirGordon
Copy link

Hello,
This example https://github.com/304NotModified/NLog-Demo-cases/tree/master/AspNetCore2WithConfigSetting
works fine unitl I try to add a Seq target with a ${configsetting}

To reproduce:

  1. Add Target.Register<SeqTarget>("Seq"); as first line in Program.cs
  2. Change second target in nlog.config to:
<target xsi:type="Seq" name="ownFile-web" serverUrl="${configsetting:name=serverUrl}">
  <property name="Source" value="Seq Target Test"/>
</target>
  1. Add "serverUrl": "http://webtest4:5341/" to appsettings.json
  2. Add <PackageReference Include="NLog.Targets.Seq" Version="1.2.0-dev-00075" /> to csproj

internal log:

Error Seq Target[ownFile-web]: Error initializing target Exception: System.UriFormatException: Invalid URI: The format of the URI could not be determined.

Am I doing something wrong?

@snakefoot
Copy link
Contributor

NLog is having a hard time with dependency injection and lazy initialization. Trying to improve on this in NLog 5.0 but until then you have to help out yourself.

Manual load configuration before loading NLog.config:

var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();

Configuration = builder.Build();

NLog.Extensions.Logging.ConfigSettingLayoutRenderer.DefaultConfiguration = Configuration;

// Now we are ready to load the NLog-config
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

Another solution is to remove all code related to NLog. So remove call to NLogBuilder and remove any creation of NLog Logger-objects until having called AddNLog() or UseNLog()

See also: NLog/NLog.Extensions.Logging#265

@snakefoot
Copy link
Contributor

Guess some magic could be sprinkled into this class, so delay the rendering of URL until start writing LogEvents.

This would have the side-effect that wrong configured SeqTarget would only be discovered when starting logging, instead of at startup.

@nblumhardt
Copy link
Member

Thanks for your advice, @snakefoot. I think, if possible, we should stick to standard NLog initialization (avoid sprinkling magic) in order to keep the sink better aligned with the rest of the NLog ecosystem, especially given the workarounds you mentioned.

@SirGordon please let us know if you're still blocked after trying the options above. Thanks for dropping by!

@SirGordon
Copy link
Author

Thank you very much for quick responses, both ways work fine.

@snakefoot
Copy link
Contributor

NLog.Web.AspNetCore ver 4.9.3 has been released, and now you can do this:

var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();

That replaces the old style:

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

@RWells-HUHS
Copy link

RWells-HUHS commented Nov 15, 2024

[Four years pass]

I have code that uses the "new" style

var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();

and this worked fine with NLog 5.3.4 under net8.0. But changing the project target to net9.0 causes this same line (with same appsettings.json and NLog.config) to throw System.UriFormatException from the LoadConfigurationFromAppSettings() call.

Suggestions?

@snakefoot
Copy link
Contributor

snakefoot commented Nov 15, 2024

@RWells-HUHS Please create new issue for your special problem, instead of awaking old issues.

NLog and Seq should ofcourse work with NET9, so for your new issue please include your appsettings.json and also enable the NLog InternalLogger and include its output. Notice it can be a deployment issue if the ServerUrl is assigned from external source like ${environment} or ${configsetting}, but further investigation can be performed in the new issue that you create.

@RWells-HUHS
Copy link

@snakefoot : Sorry for waking up old daemons. This page was the only hit on this exception and this NLog initialization call... However, I also realize now this is the home for discussions of Seq, which I'm not using. So, apologies for that, too.

Nonetheless, I appreciate your suggestion of consulting the Internal Logger. I already have it configured, but the log level is Info, so I'll make that more verbose and hopefully that will help me troubleshoot further.

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

No branches or pull requests

4 participants