作者:conan5566 2021-03-08 00:09:47
系统
分布式 在系统中,如果将日志作为文件输出,查看系统日志将非常不便;如果将日志保存到数据库中,又不能进行全文搜索。在这里我们将日志输出到ElasticSearch中,借助Kibana再查找日志。
本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。
概述
Elasticsearch可广泛应用于日志分析、全文检索、结构化数据分析等多种场景,大幅度降低维护多套专用系统的成本,在开源社区非常受欢迎。在系统中,如果将日志作为文件输出,查看系统日志将非常不便;如果将日志保存到数据库中,又不能进行全文搜索。在这里我们将日志输出到ElasticSearch中,借助Kibana再查找日志。
实现方式
1、配置es服务地址
- {
- "ConnectionStrings": {
- "ElasticSearchServerAddress": "http://localhost:9200"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*"
- }
2、配置nlog.config
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- internalLogToConsole="true">
- index="userapi-${date:format=yyyy.MM.dd}" documentType="doc" includeAllProperties="true"
- layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${logger} ${message} ${exception:format=toString}">
3、测试写入日志
- // GET api/values
- [HttpGet]
- public ActionResult
> Get() - {
- var result = new string[] { "value1", "value2" };
- _logger.LogInformation(JsonConvert.SerializeObject(result));
- return result;
- }
效果