Nlog: AppSetting Layout Renderer not read from appsettings.json

Created on 6 Sep 2017  路  12Comments  路  Source: NLog/NLog

Type: Question

NLog version:
\
\
\
\

Platform: .NET Core 2.0 Console Application

Current NLog config
\

Current appsettings.json
{
"appSettings": {
"Environment": "QA"
}
}
appsettings.json always copy

Variable env always equals DEV-{machinename}. I expected QA. Where I was mistaken and why the variable is always equal to DEV- {machinename}

All 12 comments

internal log
2017-09-06 14:33:26.2074 Error Error parsing layout appsetting will be ignored. Exception: NLog.NLogConfigurationException: Cannot access the constructor of type: NLog.LayoutRenderers.AppSettingLayoutRenderer. Is the required permission granted?
at NLog.Internal.FactoryHelper.CreateInstance(Type t)
at NLog.Config.Factory2.TryCreateInstance(String itemName, TBaseType& result) at NLog.Config.LayoutRendererFactory.TryCreateInstance(String itemName, LayoutRenderer& result) at NLog.Config.Factory2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)

after change application by https://github.com/NLog/NLog.Web/issues/34
inner log
2017-09-06 14:43:25.4078 Error Error parsing layout appsetting will be ignored. Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Could not load type 'NLog.Internal.ConfigurationManager' from assembly 'NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'.
at NLog.LayoutRenderers.AppSettingLayoutRenderer..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at PravoRu.DataLake.BanksGuarantees.Etl.Host.Program.<>c.

b__5_0(Type type) in C:\Users\1\Source\Repos\PravoRu.DataLake.GosZakupki\PravoRu.DataLake.BanksGuarantees.Etl.Host\Program.cs:line 41
at NLog.Config.Factory2.TryCreateInstance(String itemName, TBaseType& result) at NLog.Config.LayoutRendererFactory.TryCreateInstance(String itemName, LayoutRenderer& result) at NLog.Config.Factory2.CreateInstance(String name)
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, SimpleStringReader sr)

appsettings.json is not supported yet.

Is NLog.Extended work under .core 2.0? Can I create an app.config and use it?

@carkov1990 Work around is just to manual assign a NLog-global-variable to the wanted json-appsettings-value.

Is NLog.Extended work under .core 2.0?

Nope not ported yet.

@carkov1990 Hello, I created a package to access appsettings.json that worked fine under .net core2.0, you can consider it.

@linmasaki nice!

I would recommend to split your package and create 2 packages. It's better to find and the dependencies are more in control.

e.g. NLog.Appsettings.Json and NLog.AzureAppendBlob

@304NotModified Thank you for your suggestion!!! I will split the package soon(AzureAppendBlob already had). I thought this package could provide somebody who prefer all in one like me :smile:

@carkov1990 @304NotModified Hello, I had split it finished, if you are interested or needed, check this package , Thank you~

@linmasaki nice! Added it here: https://github.com/NLog/NLog/wiki/Layout-Renderers#external-packages

With NLog.Extension.Logging ver. 1.4.0 then you can now use ${configsetting}

See also: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

Was this page helpful?
0 / 5 - 0 ratings