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

AppSetting Layout Renderer not read from appsettings.json #2274

Closed
carkov1990 opened this issue Sep 6, 2017 · 12 comments
Closed

AppSetting Layout Renderer not read from appsettings.json #2274

carkov1990 opened this issue Sep 6, 2017 · 12 comments

Comments

@carkov1990
Copy link

carkov1990 commented Sep 6, 2017

Type: Question

NLog version:
<PackageReference Include="NLog" Version="5.0.0-beta09" />
<PackageReference Include="NLog.Extended" Version="4.4.0-rc1" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta5" />
<PackageReference Include="NLog.Targets.ElasticSearch" Version="4.0.0-beta21" />

Platform: .NET Core 2.0 Console Application

Current NLog config
<variable name="env" value="${whenEmpty:whenEmpty=DEV-${machinename:uppercase=true}:inner=${appsetting:name=Environment:uppercase=true}}" />

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}

@carkov1990
Copy link
Author

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)

@carkov1990
Copy link
Author

after change application by NLog/NLog.Web#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)

@304NotModified
Copy link
Member

appsettings.json is not supported yet.

@carkov1990
Copy link
Author

carkov1990 commented Sep 6, 2017

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

@snakefoot
Copy link
Contributor

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

@304NotModified
Copy link
Member

Is NLog.Extended work under .core 2.0?

Nope not ported yet.

@linmasaki
Copy link

linmasaki commented May 4, 2018

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

@304NotModified
Copy link
Member

@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

@linmasaki
Copy link

@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 😄

@linmasaki
Copy link

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

@304NotModified
Copy link
Member

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

@snakefoot
Copy link
Contributor

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

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

No branches or pull requests

4 participants