本文將介紹使用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-head
打開 http://localhost:9200/_plugin/head/,可以看如下UI,此處我們配置ip是192.168.0.103,它多語言版,已經自動識別為中文UI
在這兒我們還安裝一個管理結點的前端 bigdesk, 安裝方式類似,也是推薦插件模式:
$ ./bin/plugin -install lukas-vlcek/bigdesk/<bigdesk_version>
http://192.168.0.103:9200/_plugin/bigdesk/ 之后UI是這樣的:
還有其他的前端項目,在這兒我們不一 一 描述,其目的為了更好的管理ElasticSearch集群。
好了,我們在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里可以看到:
完整記錄JSON數據,當然也可以使用查詢方式。
接下來,讓我們來配置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.49028c.com/wintersun/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
該文章也同時發布在我的獨立博客中-Petter Liu Blog。
新聞熱點
疑難解答