使用NLog.Targets.ElasticSearch在Elasticsearch中编写的日志

时间:2018-03-20 13:07:01

标签: c# asp.net elasticsearch logging nlog

我正在尝试在我的asp.net Web API应用程序中使用目标到Elasticsearch来运行NLog。

我的应用程序的.net框架版本是4.6。

我正在使用Elasticsearch,Nest和Elasticsearch.NET V. 5.6。

NLog.Targets.ElasticSearch V 3.0.0

NLog 4.4.3

以下是我的NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
        autoReload="true"
        throwExceptions="true"
        throwConfigExceptions="true"
        internalLogLevel="Off"
        internalLogFile="c:\temp\nlog-internal.log">
  <extensions>
    <add assembly="NLog.Targets.ElasticSearch"/>
  </extensions>
  <targets>
    <target 
          name="logfile" 
          xsi:type="File" 
          fileName="Nlog.txt" />

    <target name="elastic"
              xsi:type="BufferingWrapper"
              bufferSize="1"
              flushTimeout="1">
      <target xsi:type="ElasticSearch"
              layout="${logger} | ${threadid} | ${message}"
              index="test-log-${date:format=yyyy.MM.dd}"
              includeAllProperties="true"
              uri="http://###.###.###.###:9200/">

        <field name="user"
               layout="${windows-identity:userName=True:domain=False}"/>
        <field name="host"
               layout="${machinename}"/>
      </target>
    </target>    
  </targets>

  <rules>
    <logger enabled="true" name="*" minlevel="Debug" writeTo="logfile" />
    <logger enabled="true" name="*" minlevel="Debug" writeTo="elastic" />
  </rules>
</nlog>
</configuration>

在API中,我有一个测试日志条目的方法。

private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        [AllowAnonymous]
        [HttpGet]
        public HttpResponseMessage Hello()
        {
            _logger.Info("Hello World!!!!!!!!!!!!");
            _logger.Trace("Sample trace message");
            _logger.Debug("Sample debug message");
            _logger.Info("Sample informational message");
            _logger.Warn("Sample warning message");
            _logger.Error("Sample error message");
            _logger.Fatal("Sample fatal error message");
            return Request.CreateResponse(HttpStatusCode.OK, "Hello World!");
        }

我可以在Nlog.txt文件中看到Log条目,但在Elasticsearch中没有。 我下载了 NLog.Targets.ElasticSearch 项目并附加到我的应用程序以进行调试。 调试它,我可以在下面的代码中看到

private void SendBatch(ICollection<AsyncLogEventInfo> events)
        {
            try
            {
                var logEvents = events.Select(e => e.LogEvent);

                var payload = FormPayload(logEvents);

                var result = _client.Bulk<byte[]>(payload);

                if (!result.Success)

结果.Seccess等于true。 所以没有例外,但我没有创建日志索引。 Web API已经与Elasticsearch一起正常运行。

有人可以帮我理解错误吗?我错过了什么? 提前感谢您的时间。

0 个答案:

没有答案
相关问题