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

2018-06-17 19:35:55来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

一、ElasticSearch简介


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

安装前端elasticsearch-head

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

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

image

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

$ ./bin/plugin -install lukas-vlcek/bigdesk/<bigdesk_version>
http://192.168.0.103:9200/_plugin/bigdesk/ 之后UI是这样的:
image

还有其他的前端项目,在这儿我们不一 一 描述,其目的为了更好的管理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里可以看到:

image
完整记录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请求的日志:

image

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

搜索:

image

基于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集成 , 企业信息化 等资讯,请关注我的微信订阅号:

MegadotnetMicroMsg_thumb1_thumb1_thu[1]


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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C#中堆和栈的区别分析

下一篇:Win7环境下 IIS配置