NLog.Config弹性搜索目标:
<target name="elasticWrapper" xsi:type="BufferingWrapper" flushTimeout="5000">
<target xsi:type="ElasticSearch"
name="elastic"
connectionStringName="ElasticUrl"
index="logs-${date:format=yyyy.MM.dd}"
documentType="logevent"
includeAllProperties="true"
layout="${message}">
</target>
</target>
弹性搜索目标的connectionstringname属性用于从appsettings.json获取信息
appsettings.json显示如下
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"ElasticUrl": "http://XXXXXX-XXXXXX:9200"
}
}
.Net Core项目中的Program.cs文件,如下所示
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
NLogBuilder.ConfigureNLog("nLog.config");
foreach (var setting in config.GetChildren().ToDictionary(x => x.Key, x => x.Value))
{
LogManager.Configuration.Variables[setting.Key] = setting.Value;
}
var logger = LogManager.GetCurrentClassLogger();
var host = WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseNLog()
.Build();
host.Run();
}
答案 0 :(得分:0)
尝试改变几件事
我个人在容器内使用我的应用程序。因此,我将所有设置都移到了代码中,并使用对我来说完美的环境变量来设置它们的值。
让我知道您的问题是否仍然存在。我将提供代码以通过代码配置nlog。
答案 1 :(得分:0)
我设法使其以编程方式工作。
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var logFactory = LogManager.LoadConfiguration("nLog.config");
foreach (var setting in config.GetChildren().ToDictionary(x => x.Key, x => x.Value))
{
logFactory.Configuration.Variables[setting.Key] = setting.Value;
}
var target = logFactory.Configuration.FindTargetByName("elastic") as ElasticSearchTarget;
target.Uri = config.GetConnectionString("ElasticUrl");
NLogBuilder.ConfigureNLog("nLog.config");
var logger = LogManager.GetCurrentClassLogger();
var host = WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseNLog()
.Build();
host.Run();
}
答案 2 :(得分:0)
扩展@Sahadevan的答案。
如果您使用的是BufferingWrapper,则findTargetByName需要分两步进行。
var target = logFactory.Configuration.FindTargetByName("elastic") as BufferingTargetWrapper;
var esTarget = target.WrappedTarget as ElasticSearchTarget;
示例配置:
<target xsi:type="BufferingWrapper" name="elastic"
flushTimeout="5000">
<target xsi:type="ElasticSearch"
includeAllProperties ="true">
</target>
</target>