Closed bluetianx closed 5 years ago
The frontpage https://github.com/markmcdowell/NLog.Targets.ElasticSearch suggests that you add the following to the top of the nlog.config:
<extensions>
<add assembly="NLog.Targets.ElasticSearch"/>
</extensions>
OK ,thanks, It has worked
one more thing,i have got a exception that Error initializing target Exception: System.ArgumentException: Argument can not be an empty collection when I want set ESUrl by appsetting,
exception info as
2019-07-26 08:55:06.4425 Error ElasticSearch Target[ElasticSearch]: Error initializing target Exception: System.ArgumentException: Argument can not be an empty collection
Parameter name: nodes
at Elasticsearch.Net.Extensions.ThrowIfEmpty[T](IEnumerable`1 object, String parameterName)
at Elasticsearch.Net.StaticConnectionPool..ctor(IEnumerable`1 nodes, Func`2 nodeScorer, IDateTimeProvider dateTimeProvider)
at Elasticsearch.Net.StaticConnectionPool..ctor(IEnumerable`1 uris, Boolean randomize, IDateTimeProvider dateTimeProvider)
at NLog.Targets.ElasticSearch.ElasticSearchTarget.InitializeTarget()
at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
my appsetting as
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ESUrl": "http://IP:9200"
}
my nlog xml as
<?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="logs/internal-nlog.txt">
<extensions>
<add assembly="NLog.Targets.ElasticSearch"/>
</extensions>
<!-- the targets to write to -->
<targets>
<target name="ESErrorLog" xsi:type="RetryingWrapper" retryDelayMilliseconds="2000" retryCount="3">
<target xsi:type="ElasticSearch"
uri="${configsetting:name=ESUrl}"
index="NlogTest"
includeAllProperties="true"
disableAutomaticProxyDetection="false"
>
<!-- repeated, optional -->
</target>
</target>
<target name="ESGeneralLog" xsi:type="BufferingWrapper" flushTimeout="5000">
<target xsi:type="ElasticSearch"
uri="${configsetting:name=ESUrl}"
index="NlogTest"
includeAllProperties="true"
disableAutomaticProxyDetection="false"
>
<!-- repeated, optional -->
</target>
</target>
</targets>
<rules>
<logger name="*" minlevel="Warn" writeTo="ESErrorLog" />
<logger name="*" minlevel="Trace" writeTo="ESGeneralLog" />
</rules>
</nlog>
Maybe the NLog.config is being loaded before having fully initialized the ConfigSetting (so it returns empty value).
See also: https://github.com/NLog/NLog.Extensions.Logging/issues/265
And see also: https://github.com/NLog/NLog/wiki/Reinitialize-NLog-configuration
ok . thanks
NLog.Web.AspNetCore ver 4.9.3 has been released, and now you can do this:
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
That replaces the old style:
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
I have a problem when i use ElasticSearch in asp.net core;
my code as
my nlog xml as :
my csproj as