具有弹性搜索目标的Nlog ASP.Net Core无法正常工作

时间:2018-09-20 10:47:22

标签: c# elasticsearch asp.net-core nlog

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();     

}

3 个答案:

答案 0 :(得分:0)

尝试改变几件事

  1. 添加文件目标,以查看您的配置是否被Nlog识别。
  2. 将Nlog.config重命名为nlog.config并将文件属性值标记为始终复制到输出目录。
  3. 确保已将NLog.Targets.ElasticSearch nuget包添加到项目中。

我个人在容器内使用我的应用程序。因此,我将所有设置都移到了代码中,并使用对我来说完美的环境变量来设置它们的值。

让我知道您的问题是否仍然存在。我将提供代码以通过代码配置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>