dll引用:
NLog.Targets.ElasticSearch,版本:4.0.0-beta26
Nlog,版本:4.5.0-rc04
Microsoft.Extensions.Configuration,版本:2.0.0
appsettings.json(属性=》复制到输出目录:始终复制),ElasticSearch的url配置
{
"ElasticSearchServerAddress": "http://*.*.*.*:9201/"
}
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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
>
<!--ElasticSearch保存日志,必须添加此配置-->
<extensions>
<add assembly="NLog.Targets.ElasticSearch"/>
</extensions> <targets async="true">
<!--本地目录存储日志文件-->
<target xsi:type="File" name="File" fileName="${environment:variable=NLOGDIR}/FinancialBill-Inbound${date:format=yyyy-MM-dd}.log"
archiveAboveSize="20000000" archiveNumbering="Rolling" encoding="UTF-8" enableFileDelete="true" maxArchiveFiles="5"
layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${logger} ${message} ${exception:format=toString}" />
<!--控制台,输出日志信息-->
<target xsi:type="ColoredConsole" name="ColoredConsole" layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${message} ${exception:format=toString}"/>
<!--ElasticSearch保存日志信息-->
<target name="ElasticSearch" xsi:type="ElasticSearch" ConnectionStringName="ElasticSearchServerAddress" index="dvm-report-financialbill-inbound-${date:format=yyyy.MM.dd}" documentType="logevent" includeAllProperties="true"
layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${logger} ${message} ${exception:format=toString}">
<field name="MachineName" layout="${machinename}" />
<field name="Time" layout="${longdate}" />
<field name="level" layout="${level:uppercase=true}" />
<field name="logger" layout=" ${logger}" />
<field name="message" layout=" ${message}" />
<field name="exception" layout=" ${exception:format=toString}" />
<field name="processid" layout=" ${processid}" />
<field name="threadname" layout=" ${threadname}" />
<field name="stacktrace" layout=" ${stacktrace}" />
<field name="Properties" layout="${machinename} ${longdate} ${level:uppercase=true} ${logger} ${message} ${exception}|${processid}|${stacktrace}|${threadname}" />
</target>
</targets> <rules>
<logger name="*" minlevel="Trace" writeTo="File" />
<logger name="*" minlevel="Trace" writeTo="ColoredConsole" />
<logger name="*" minlevel="Trace" writeTo="ElasticSearch" />
</rules> </nlog>
${environment:variable=NLOGDIR}:环境变量NLOGDIR的值
${appsetting:ElasticSearch}:appsetting.config中的ElasticSearch的值