When the NLog configuration is invalid LogManager.GetCurrentClassLogger throws an exception, even when throwsException is false (which is the default). NLog doesn't log this error to its internal error log file.
So, for the case of an invalid configuration I have to create some fallback mechanism that still allows me to log this error. Specifically, this means that I have to encapsulate the creation of the logger in a try catch block and either use low level means to create my own log file or create an NLog instance with default values in code.
Is this intended behavior?
Comments: ** Comment from web user: PapaCoen **
Hope you still look at these issues: I seem to have the same problem with NLog 3.1.0.0. When I use ${machine} instead of ${machinename} I get the following error:
```
System.Configuration.ConfigurationErrorsException: An error oc
curred creating the configuration section handler for nlog: Error when setting p
roperty 'FileName' on File Target[file] (...Export.exe.config line 11) ---> NLog.NLogCo
nfigurationException: Error when setting property 'FileName' on File Target[file
] ---> System.ArgumentException: LayoutRenderer cannot be found: 'machine'
at NLog.Config.Factory`2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory con
figurationItemFactory, SimpleStringReader sr)
at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configura
tionItemFactory, SimpleStringReader sr, Boolean isNested, String& text)
at NLog.Layouts.SimpleLayout.set_Text(String value)
at NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type propertyType,
String value, Object& newValue, ConfigurationItemFactory configurationItemFactor
y)
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name,
String value, ConfigurationItemFactory configurationItemFactory)
--- End of inner exception stack trace ---
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name,
String value, ConfigurationItemFactory configurationItemFactory)
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object t
argetObject, NLogXmlElement element, Boolean ignoreType)
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLog
XmlElement targetElement)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement tar
getsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogEl
ement, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fi
leName, Boolean ignoreErrors)
at NLog.Config.XmlLoggingConfiguration..ctor(XmlElement element, String fileN
ame)
at NLog.Config.ConfigSectionHandler.Create(XmlNode section, IAppDomain appDom
ain)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactor
y.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord facto
ryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader read
er)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactor
y.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord
, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig,
ConfigXmlReader reader)
at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inpu
tIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object par
entConfig, ConfigXmlReader reader)
at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inp
utIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object pa
rentConfig, ConfigXmlReader reader, String filename, Int32 line)
--- End of inner exception stack trace ---
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, Se
ctionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord
sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factor
yRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boole
an getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String co
nfigKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Bool
ean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String co
nfigKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Bool
ean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String co
nfigKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Bool
ean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at NLog.LogFactory.get_Configuration()
at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
at NLog.LogManager.GetCurrentClassLogger()
at
```
Took me half a day to figure out it was an error with the NLog configuration :)