.netcore3.1使用ELK日志中心(使用NLog数据传输)
1、ELK部署
ELK是elasticsearch、kibana、Logstash组合而成的整套解决方案,前面我们使用skywalking时候已经部署好elasticsearch(参见上一篇随笔),接下来只需要安装Logstash和kibana即可
1.1、安装kibana
前面使用的elasticsearch是7.5.0版本,这里kibana选择相同版本:
运行:
docker run -d --restart=always --name kibana -e ELASTICSEARCH_URL=http://192.168.56.10:9200 -p 5601:5601 kibana:7.5.0
浏览器访问:
进入容器查看配置文件:
docker exec -it kibana /bin/bash
将配置文件中的http://elasticsearch:9200改为http://192.168.56.10:9200
重启容器:
docker restart kibana
还是出现问题则稍等1分钟,重试,页面正常加载:
1.2、安装Logstash
同样选择7.5.0版本:
sudo docker pull logstash:7.5.0
1.2.1、启动
docker run -d --name=logstash -p 5044:5044 logstash:7.5.0
查看日志:
docker logs -f logstash
进入容器
docker exec -it logstash /bin/bash
修改配置文件:
vi config/logstash.yml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://192.168.56.10:9200" ]
vi pipeline/logstash.conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
input {
tcp {
port => 5044
}
}
filter {
grok {
match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" }
}
}
output {
elasticsearch {
hosts => "192.168.56.10:9200"
index => "sample"
}
stdout {
codec => rubydebug
}
}
重启:
docker restart logstash
2、NetCore 使用 ELK
2.1、添加NLog、NLog.Web.AspNetCore nuget包
2.2、根目录添加配置文件nlog.config
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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"/> </extensions> <!-- define various log targets --> <!--定义日志文件目录--> <variable name="logDirectory" value="${basedir}/logs/${shortdate}"/> <variable name="nodeName" value="node1"/> <targets async="true"> <!-- 全部日志target --> <target xsi:type="File" name="allfile" fileName="${logDirectory}/nlog-all/${shortdate}.log" layout="#node1#${longdate}#${logger}#${uppercase:${level}}#${callsite}#${callsite-linenumber}#${aspnet-request-url}#${aspnet-request-method}#${aspnet-mvc-controller}#${aspnet-mvc-action}#${message}#${exception:format=ToString}#" keepFileOpen="false" /> <!-- 本地文件日志target --> <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="false" /> <!-- Tcp日志target --> <target xsi:type="Network" name="ownLog-tcp" keepConnection="false" address ="tcp://192.168.56.10:5044" 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}#" /> <!--grok 规则--> <!--%#{DATA:request_time}#%{DATA:node_name}#%{DATA:class_name}#%{DATA:log_level}#%{DATA:call_site}#%{DATA:line_number}#%{DATA:request_url}#%{DATA:request_method}#%{DATA:container_name}#%{DATA:action_name}#%{DATA:log_info}#%{DATA:exception_msg}#--> <!--空白--> <target xsi:type="Null" name="blackhole" /> </targets> <!--日志级别 Trace -》Debug-》 Info -》Warn-》 Error-》 Fatal--> <!--日志规则--> <rules> <!--全部日志, 包括Microsoft日志--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--自定义日志,排除Microsoft日志--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Debug" writeTo="ownLog-file" /> <logger name="*" minlevel="Info" writeTo="ownLog-tcp" /> </rules> </nlog>
2.3、修改Program
在最后增加配置
ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
}).UseNLog();
2.4、修改Startup
var currentClassLogger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); services.AddSingleton(currentClassLogger);
2.5、写入测试(接口必须访问几遍,不然下面Kibana面板上无数据)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class HealthCheckController : ControllerBase { private readonly ILogger<HealthCheckController> _logger; public HealthCheckController(ILogger<HealthCheckController> logger) { _logger = logger; } [HttpGet] public IActionResult GetHealthCheck() { _logger.LogInformation("这是一条测试消息Information"); _logger.LogError("这是一条测试消息Erro"); _logger.LogDebug("这是一条测试消息Debug"); return Ok("连接正常"); } }
3、Kibana查看数据
先看结果:
3.1、首先创建索引模板:
Logstatsh配置文件logstash.conf中配置的output属性:
Index patterns这里同样输入sample*
其他输入项目前还没搞清楚是干嘛的,反正是可选项,直接下一步,完成创建即可。
3.2、根据索引模板查看数据
首页即会提示创建,或者点击最左上角图标到主页,再点击
配置页,输入sample*,直接下一步即可(全部按默认创建,显示所有字段即可)
3.3、点击Discover
可以看到上面写入的日志记录了
到这里,基本的日志写入和呈现就完成了。
参考:
https://blog.csdn.net/xiaozm1223/article/details/89475003
https://www.cnblogs.com/chongyao/p/14098197.html
https://www.cnblogs.com/Leo_wl/p/11796634.html