NLog failed to parse config file - Target Console not found.

Created on 6 Dec 2018  路  19Comments  路  Source: NLog/NLog

Hi! Thanks for reporting this feature/bug/question!

Please keep / fill in the relevant info from this template so that we can help you as best as possible.

QUESTIONS are preferred on StackOverflow. You could expect a faster response there (but do include all the info below). Use the tag "nlog" https://stackoverflow.com/questions/ask

For .NET Core users, please check the platform support: https://github.com/NLog/NLog/wiki/platform-support

NLog version: (e.g. 4.4.13) 4.5.11

Platform: .NET Core 2

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

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>

      <target xsi:type="Console"
          name="String"
          detectConsoleAvailable="false"
          layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"/>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="Console" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Trace" writeTo="Console" />
  </rules>
</nlog>

  • What is the current result?
    App start, but no logs are written
  • What is the expected result?
    Logs are written to console
  • Did you checked the Internal log?
2018-12-06 16:02:45.4642 Info Message Template Auto Format enabled
2018-12-06 16:02:45.4816 Info Loading assembly: NLog.Web.AspNetCore
2018-12-06 16:02:45.5330 Info Adding target Console Target[String]
2018-12-06 16:02:45.5639 Error Parsing configuration from <>/nlog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing <>/nlog.config.  ---> NLog.NLogConfigurationException: Target Console not found.
   at NLog.Config.XmlLoggingConfiguration.ParseLoggerElement(NLogXmlElement loggerElement, IList`1 rulesCollection)
   at NLog.Config.XmlLoggingConfiguration.ParseRulesElement(NLogXmlElement rulesElement, IList`1 rulesCollection)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---
2018-12-06 16:02:45.5652 Warn Failed loading config from <>/nlog.config. Invalid XML?
2018-12-06 16:02:45.5734 Info Found 15 configuration items
  • Please post full exception details (message, stacktrace, inner exceptions)
  • Are there any workarounds? no
  • Is there a version in which it did work?
  • Can you help us by writing an unit test? yes
bug

All 19 comments

Could seem like you are using NLog for NetStandard1.3 (UWP).Console should be available with NLog NetStandard 1.5 + 2.0. Could you verify the file-properties of the NLog.dll in use?

"NLog/4.5.11": {
        "runtime": {
          "lib/netstandard2.0/NLog.dll": {
            "assemblyVersion": "4.0.0.0",
            "fileVersion": "4.5.11.8645"
          }
        },
        "compile": {
          "lib/netstandard2.0/NLog.dll": {}
        }
      },

This one?

Looks good. Are you on some platform (Like UWP, Xamarin, NetNative) where you perform ahead-of-time (AOT) compiling of code to native?

Not that I'm aware of. MacOS using regular dotnet core.

What happens if you perform the configuration using code:

c# var config = new NLog.Config.LoggingConfiguration(); var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole); NLog.LogManager.Configuration = config;

Maybe for some reason then it might use the iOS-assembly instead of the wanted NetStandard. Or maybe Xamarin-build is doing some linker-tree-shake that removes everything not used directly.

Not an experienced MacOS-developer. Can only say that NLog works just fine with console on NetCore2

Yep, applying configuration from code is working. So weird...

Yep, applying configuration from code is working. So weird...

Then your build-environment is doing ahead-of-time (AOT) compiling of code with linker-tree-shake. You need to modify your build to ignore NLog.dll when stripping away "unnecessary code" .

Can you help me understand how to do that?

Maybe you can add the following "Skip linking assemblies"-option at Project Options -> Build:

--linkskip=NLog

See also https://xamarinhelp.com/xamarin-linker/ and https://www.mvvmcross.com/documentation/fundamentals/linking

I guess if NLog is ever going to be used on devices where AOT-compile is mandatory without support for reflection (Ex. iOS-Phone-platform) then NLog need to improve on its runtime config-API (Also when using NLog-LayoutRenderers)

Created #3039 for NLog 4.6 that should fix the problem with Xamarin Linker removing everything from NLog.dll Allowing NLog.config-files to work without needing to specify --linkskip=NLog

@omerlh If you know how to setup a local nuget-package-repository, then you are very welcome to try this pre-release build to see if it works as expected (without using --linkskip=NLog):

https://ci.appveyor.com/project/nlog/nlog/builds/20883080/artifacts (Download NLog.4.5.11.8779-PR3039.nupkg)

I tried, but look like I can't use this package:

error: Package 'NLog.4.5.11.8779-PR3039.nupkg' is incompatible with 'all' frameworks in project <>.

Also couldn't figure out where should I put --linkskip=NLog.

@omerlh Try rename the package from NLog.4.5.11.8779-PR3039.nupkg to NLog.4.5.11.8779.nupkg (And retry the install)

It might help as someone says here: https://github.com/NuGet/Home/issues/5127 (Have no issues myself using these package-names in VS2017)

3039 has been merged, so assume this is fixed. Will reopen if not!

I'm facing the issue of config file not loading to the logmanager.configuration. due to this the the config object has not targets in it whereas i have added the rules n targets in the nlog.config file.
Package is Nlog 4.6.1. the nlog.config file is android asset. also the code is portable project

I'm facing the issue of config file not loading to the logmanager.configuration. due to this the the config object has not targets in it whereas i have added the rules n targets in the nlog.config file.
Package is Nlog 4.6.1. the nlog.config file is android asset. also the code is portable project

I assume this is https://github.com/NLog/NLog/issues/3283

NLog.config can be added as an Xamarin resource, which is automatically deployed but has to be loaded manually:

https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading#loading-nlog-configuration-from-xamarin-resource

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FaMouZx3 picture FaMouZx3  路  3Comments

BobSeu picture BobSeu  路  3Comments

JustArchi picture JustArchi  路  3Comments

carkov1990 picture carkov1990  路  3Comments

Jerefeny picture Jerefeny  路  3Comments