本文將介紹使用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。