NLog / NLog

NLog - Advanced and Structured Logging for Various .NET Platforms
https://nlog-project.org
BSD 3-Clause "New" or "Revised" License
6.31k stars 1.37k forks source link

NLog throws System.Configuration.ConfigurationErrorsException if appsettings not valid #1772

Closed nmklotas closed 7 years ago

nmklotas commented 7 years ago

Hello, I have an issue with invalid appsettings file and NLog.

NLog version: (e.g. 4.3.10)

Platform: ..Net 4.5 Current NLog config No config file is used

NLog method:

LogManager.GetCurrentClassLogger();

throws exception.

NLog does not use applications settings file, and I as a developer can handle App failure if user.config is not valid when accessing application settings.

HResult=-2146233088
Message=An exception was thrown while invoking the constructor 'Void .ctor()' on type 'Logger'. ---> Configuration system failed to initialize (See inner exception for details.)
Source=Autofac
StackTrace:
     at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
     at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
     at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
InnerException: 
     BareMessage=Configuration system failed to initialize
     HResult=-2146232062
     Line=0
     Message=Configuration system failed to initialize
     Source=System.Configuration
     StackTrace:
          at System.Configuration.ConfigurationManager.PrepareConfigSystem()
          at System.Configuration.ConfigurationManager.GetSection(String sectionName)
          at NLog.Config.XmlLoggingConfiguration.get_AppConfig()
          at NLog.LogFactory.get_Configuration()
          at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
          at NLog.LogManager.GetCurrentClassLogger()
          at WindowsApp.App.Logger..ctor() in C:\Users\[user]\Source\Repos\App\Logger.cs:line 12
          at lambda_method(Closure , Object[] )
          at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
     InnerException: 
          BareMessage=The 'setting' start tag on line 26 position 14 does not match the end tag of 'value'. Line 27, position 57.
          Filename=C:\Users\[user]\AppData\Local\App\App.exe_Url_z5vppy3gwaknsbti1tuken5tdwf313mm\1.0.7.0\user.config
          HResult=-2146232062
          Line=27
          Message=The 'setting' start tag on line 26 position 14 does not match the end tag of 'value'. Line 27, position 57. (C:\Users\[user]\AppData\Local\App\App.exe_Url_z5vppy3gwaknsbti1tuken5tdwf313mm\1.0.0.0\user.config line 27)
          Source=System.Configuration
          StackTrace:
               at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
               at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
               at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
          InnerException: 
               HResult=-2146232000
               LineNumber=27
               LinePosition=57
               Message=The 'setting' start tag on line 26 position 14 does not match the end tag of 'value'. Line 27, position 57.
               Source=System.Xml
               SourceUri=""
               StackTrace:
                    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
                    at System.Xml.XmlTextReaderImpl.ThrowTagMismatch(NodeData startTag)
                    at System.Xml.XmlTextReaderImpl.ParseEndElement()
                    at System.Xml.XmlTextReaderImpl.ParseElementContent()
                    at System.Xml.XmlTextReaderImpl.Skip()
                    at System.Configuration.XmlUtil.StrictSkipToNextElement(ExceptionAction action)
                    at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
                    at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
                    at System.Configuration.BaseConfigurationRecord.ScanSections(XmlUtil xmlUtil)
                    at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
               InnerException: 
304NotModified commented 7 years ago

is your NLog config in nlog.config?

Could you try with <nlog throwConfigExceptions="false"> ?

nmklotas commented 7 years ago

Hi,

I don't have any NLog configuration file. Should I have one?

304NotModified commented 7 years ago

No perse, so the nlog config is in the appSettings? Or there isn't a log config at all?

nmklotas commented 7 years ago

There is no nlog config at all. I deleted it long time ago.

304NotModified commented 7 years ago

I see. One fix is to disable throwing of config exception, I think a work around would be to set a new config before getting the logger:

LogManager.Configuration= new LoggingConfiguration();// before all GetCurrentClassLogger
LogManager.GetCurrentClassLogger();
sllewellyn13 commented 6 years ago

I am experiencing this problem ever since I added an app.config to my solution.

sllewellyn13 commented 6 years ago

When trying the suggested solution above, I get: System.TypeInitializationException: 'The type initializer for 'Nlog.Common.InternalLogger' threw an exception.' 2 of 2 Inner Exceptions ConfigurationErrorsException: Unrecognized attribute 'xmlns:xdt'. Note that attribute names are case sensitive.

We are using transforms in our builds so using the transforms with an app.config is new, too. It is a requirement of my organization. I may have to get rid of NLog.

304NotModified commented 6 years ago

Please open a new issue, fill in the template and reference to this one, thanks!