星空网 > 软件开发 > ASP.net

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

  本文将介绍使用NLOG、Elmah结合ElasticSearch实现分布式日志管理。

一、ElasticSearch简介


ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,
能够达到实时搜索,稳定,可靠,快速,安装使用方便。 建立一个网站或应用程序,并要添加搜索功能,令我们受打击的
是:搜索工作是很难的。希望我们的搜索解决方案要快,希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单
地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们
要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。
ElasticSearch的Schema与其它DB比较:
ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理
ElasticSearch三方访问方式:
ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理
  环境是CentOS6.4,安装方法有好几种,在这儿我们直接从官网下载包, 1.71版解压后,进入目录执行:
  bin/elasticsearch
  检查服务是否正常工作
  curl -X GET http://localhost:9200/
elasticsearch默认是9200端口,返回一个JSON数据,有版本说明运行正常。 
elasticsearch的伸缩性很高,如下示例数据分片:
ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

安装前端elasticsearch-head

elasticsearch/bin/plugin –install  mobz/elasticsearch-head

打开 http://localhost:9200/_plugin/head/,可以看如下UI,此处我们配置IP是192.168.0.103,它多语言版,已经自动识别为中文UI

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

在这儿我们还安装一个管理结点的前端 bigdesk,  安装方式类似,也是推荐插件模式:

$ ./bin/plugin -install lukas-vlcek/bigdesk/<bigdesk_version>
http://192.168.0.103:9200/_plugin/bigdesk/ 之后UI是这样的:
ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

还有其他的前端项目,在这儿我们不一 一 描述,其目的为了更好的管理ElasticSearch集群。

 

二、ElasticSearch与Asp.net应用程序集成

好了,我们在Asp.net项目中已经安装Elmah,现在我们安装 Elmah.Elasticsearch,这里是1.1.0.27

PM> Install-Package Elmah.Elasticsearch

在web.config中配置节,我们配置index名称:elmahCurrent

 <elmah>  <!--    See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for    more information on remote access and securing ELMAH.  --><security allowRemoteAccess="true" />  <errorLog type="Elmah.Io.ElasticSearch.ElasticSearchErrorLog, Elmah.Io.ElasticSearch" connectionStringName="ElmahIoElasticSearch" defaultIndex="elmahCurrent" /></elmah>

连接字符串增加

 <connectionStrings>  <add name="ElmahIoElasticSearch" connectionString="http://192.168.0.103:9200/" /> </connectionStrings>

让我们来访问一个不存在http://localhost:1960/KK webpage  故意引发异常,然后我们到前端head里可以看到:

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理
完整记录JSON数据,当然也可以使用查询方式。

接下来,让我们来配置NLOG的日志也输出到ElasticSearch,先安装包 NLog.Targets.ElasticSearch 1.0.14

PM> Install-Package NLog.Targets.ElasticSearch

对应的NLog.config文件是这样的,看加粗字体:

<?  <target name="elastic" xsi:type="ElasticSearch" uri="http://192.168.0.103:9200/" index="DevLogging" documentType="logevent">  </target>  <target name="asyncFile" xsi:type="AsyncWrapper">   <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"    layout="${longdate} ${logger} ${uppercase:${level}} ${message} ${exception:format=ToString,StackTrace,method:maxInnerExceptionLevel=5:innerFormat=ToString}" />  </target> </targets> <rules>  <logger name="*" minlevel="Trace" writeTo="f" />   <logger name="*" minlevel="Trace" writeTo="elastic" /> </rules></nlog>

这样我们可以把非异常的日志自由输出到ElasticSearch中,例如我们记录webapi请求的日志:

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

devlogging是我们在配置文件已配置过的index名称。 我们同时使用NLOG记录了文件日志。

搜索:

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

基于REST方式请求按ID查询:

http://localhost:9200/<index>/<type>/<id>.

如:

http://192.168.0.103:9200/devlogging/logevent/AU9a4zu6oaP7IVhrhcmO

还有一些搜索示例如下:

//索引
$ curl -XPUT http://localhost:9200/twitter/tweet/2 -d '{
    "user": "kimchy",
    "post_date": "2009-11-15T14:12:12",
    "message": "You know, for Search"
}'

//lucene语法方式的查询
$ curl -XGET http://localhost:9200/twitter/tweet/_search?q=user:kimchy

//query DSL方式查询
$ curl -XGET http://localhost:9200/twitter/tweet/_search -d '{
    "query" : {
        "term" : { "user": "kimchy" }
    }
}'

//query DSL方式查询
$ curl -XGET http://localhost:9200/twitter/_search?pretty=true -d '{
    "query" : {
        "range" : {
            "post_date" : {
                "from" : "2009-11-15T13:00:00",
                "to" : "2009-11-15T14:30:00"
            }
        }
    }
}'

我们可以配置多个应用程序的日志统一输出到ES中,以便于我们查询与分析。

今天先这儿,希望对您有软件开发帮助。


来资料收集与整合,希望对您软件开发与企业信息化有帮助。 其它您可能感兴趣的文章:
N-Tier Entity Framework开源项目介绍
IT基础架构规划方案一(网络系统规划)
IT基础架构规划方案二(计算机系统与机房规划规划) 
IT基础架构规划方案三(IT基础软件和系统规划)
企业应用之性能实时度量系统演变
云计算参考架构几例
智能移动导游解决方案简介
人力资源管理系统的演化

如有想了解更多软件研发 , 系统 IT集成 , 企业信息化 等资讯,请关注我的微信订阅号:

ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog。




原标题:ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

“全民健身”催生新风口,小白卖家深耕Shopee一年即爆单!:https://www.kjdsnews.com/a/1545642.html
SHEIN巴西站已吸引百万用户;Temu计划开放美国本土卖家入驻:https://www.kjdsnews.com/a/1545643.html
跨境独立站要怎么做?哪种方式更适合新手建站?:https://www.kjdsnews.com/a/1545644.html
谁能挑战拼多多?:https://www.kjdsnews.com/a/1545645.html
墨西哥CFDI 4.0发票生效后,你的发票抵扣权利是否受到影响?:https://www.kjdsnews.com/a/1545646.html
单量持续增长!一批卖家开始冲刺下半年:https://www.kjdsnews.com/a/1545647.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流