本文将介绍使用NLOG、Elmah结合ElasticSearch实现分布式日志管理。
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
Elasticsearch是用Java开发的,并做为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,
可以达到实时搜索,稳定,可靠,快速,安装使用方便。 创建一个网站或应用程序,并要添加搜索功能,令咱们受打击的
是:搜索工做是很难的。但愿咱们的搜索解决方案要快,但愿有一个零配置和一个彻底免费的搜索模式,咱们但愿可以简单
地使用JSON经过HTTP的索引数据,咱们但愿咱们的搜索服务器始终可用,咱们但愿可以一台开始并扩展到数百,咱们
要实时搜索,咱们要简单的多租户,咱们但愿创建一个云的解决方案。Elasticsearch旨在解决全部这些问题和更多的问题。
ElasticSearch的Schema与其它DB比较:
ElasticSearch三方访问方式:
环境是CentOS6.4,安装方法有好几种,在这儿咱们直接从官网下载包, 1.71版解压后,进入目录执行:
bin/elasticsearch
检查服务是否正常工做
curl -X GET http://localhost:9200/
elasticsearch默认是9200端口,返回一个JSON数据,有版本说明运行正常。
elasticsearch的伸缩性很高,以下示例数据分片:
安装前端elasticsearch-head
elasticsearch/bin/plugin –install mobz/elasticsearch-headhtml
打开 http://localhost:9200/_plugin/head/,能够看以下UI,此处咱们配置IP是192.168.0.103,它多语言版,已经自动识别为中文UI前端
在这儿咱们还安装一个管理结点的前端 bigdesk, 安装方式相似,也是推荐插件模式:git
$ ./bin/plugin -install lukas-vlcek/bigdesk/<bigdesk_version>
http://192.168.0.103:9200/_plugin/bigdesk/ 以后UI是这样的:
还有其余的前端项目,在这儿咱们不一 一 描述,其目的为了更好的管理ElasticSearch集群。github
好了,咱们在Asp.net项目中已经安装Elmah,如今咱们安装 Elmah.Elasticsearch,这里是1.1.0.27web
PM> Install-Package Elmah.Elasticsearchapache
在web.config中配置节,咱们配置index名称:elmahCurrentapi
<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里能够看到:微信
接下来,让咱们来配置NLOG的日志也输出到ElasticSearch,先安装包 NLog.Targets.ElasticSearch 1.0.14
PM> Install-Package NLog.Targets.ElasticSearch
对应的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"> <extensions> <add assembly="NLog.Targets.ElasticSearch"/> </extensions> <targets async="true"> <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请求的日志:
devlogging是咱们在配置文件已配置过的index名称。 咱们同时使用NLOG记录了文件日志。
搜索:
基于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集成 , 企业信息化 等资讯,请关注个人微信订阅号:
做者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。
该文章也同时发布在个人独立博客中-Petter Liu Blog。