markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

Logs not write to Elastic in aspnet.core #92

Closed chenzhe014 closed 4 years ago

chenzhe014 commented 5 years ago

i want to add fileds in my logs from elasticsearch .

var myLogger = LogManager.GetCurrentClassLogger();
var eventInfo = new LogEventInfo(NLog.LogLevel.Info, myLogger.Name, sb.ToString());
eventInfo.Properties["Tag"] = dic["tag"];
myLogger.Log(eventInfo);

But it will only be recorded in local files,and will not be recorded in elasticsearch. when i append logger.LogInformation("n")(Microsoft.Extensions.Logging) . it will be recorded in elasticsearch. I have tried this many times and this is always the result.

Log Code:

var myLogger = LogManager.GetCurrentClassLogger();
var eventInfo = new LogEventInfo(NLog.LogLevel.Info, myLogger.Name, sb.ToString());
eventInfo.Properties["Tag"] = dic["tag"];
myLogger.Log(eventInfo);
//logger.LogInformation("n");

Nlog.Config

<?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="Warn"
      internalLogFile="internal-nlog.txt">

  <extensions>
    <!--enable NLog.Web for ASP.NET Core-->
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="NLog.Targets.ElasticSearch"/>
    <add assembly="NLog.Qunar"/>
  </extensions>

  <!-- define various log targets -->
  <variable name="logDirectory" value="${basedir}/logs/${shortdate}"/>
  <variable name="nodeName" value="node1"/>

  <targets async="true">
    <default-wrapper xsi:type="BufferingWrapper" bufferSize="5"/>
    <target xsi:type="File"
            name="allfile"
            fileName="${logDirectory}/nlog-all/${shortdate}.log"
            layout="[${longdate}][${machinename}][${level}] ${message} ${exception}"
            keepFileOpen="false"
            concurrentWrites="false"
            archiveAboveSize ="10485760"
            />

    <target xsi:type="File"
            name="ownLog-file"
            fileName="${logDirectory}/nlog-${level}/${shortdate}.log"
            layout="#${longdate}#${nodeName}#${logger}#${uppercase:${level}}#${callsite}#${callsite-linenumber}#${aspnet-request-url}#${aspnet-request-method}#${aspnet-mvc-controller}#${aspnet-mvc-action}#${message}#${exception:format=ToString}#"
            keepFileOpen="true"
            concurrentWrites="false"
            archiveAboveSize ="10485760"
            />

    <target xsi:type="Null" name="blackhole" />

    <target name="elastic" xsi:type="BufferingWrapper" flushTimeout="5000">
      <target xsi:type="ElasticSearch" layout="${logger} | ${threadid} | ${message}" index="qunar_api" includeAllProperties="true" requireAuth="true" username="****" password="******" uri="http://localhost:9210">
        <field name="user" layout="${windows-identity:userName=True:domain=False}"/>
        <field name="host" layout="${machinename}"/>
        <field name="number" layout="1" layoutType="System.Int32"/>
      </target>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Info" writeTo="elastic" />
  </rules>
</nlog>
snakefoot commented 5 years ago

Instead of jumping into the deep end of the pool, then try something simple.

Forexample this setting will carry a lot of suprises:

<default-wrapper xsi:type="BufferingWrapper" bufferSize="5"/>

Instead try this NLog.config instead:


<?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="Warn"
      internalLogFile="internal-nlog.txt">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="NLog.Targets.ElasticSearch"/>
  </extensions>

  <!-- define various log targets -->
  <variable name="logDirectory" value="${basedir}/logs/${shortdate}"/>

  <targets async="true">
    <target xsi:type="File"
            name="allfile"
            fileName="${logDirectory}/nlog-all/${shortdate}.log"
            keepFileOpen="true"
            concurrentWrites="false"
            />

    <target xsi:type="File"
            name="ownLog-file"
            fileName="${logDirectory}/nlog-${level}/${shortdate}.log"
            keepFileOpen="true"
            concurrentWrites="false"
            />

    <target xsi:type="ElasticSearch" name="elastic" layout="${logger} | ${threadid} | ${message}" index="qunar_api" includeAllProperties="true" requireAuth="true" username="****" password="******" uri="http://localhost:9210" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <logger name="*" minlevel="Info" writeTo="elastic" />
  </rules>
</nlog>

Have you checked the output of the NLog InternalLogger for any issues? See also https://github.com/NLog/NLog/wiki/Internal-Logging

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.