zoukankan      html  css  js  c++  java
  • ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

       本文将介绍使用NLOGElmah结合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

  • 相关阅读:
    A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问
    Fast Newman-FN算法以及模块度定义介绍
    Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
    设计一个smartnic
    Intel GEN11 GPU
    Intel GEN9 GPU
    Shared Virtual Memory (SVM) Functions
    connect via ssh to virtualbox guest vm without knowing ip address
    smartnic
    技术精品翻译
  • 原文地址:https://www.cnblogs.com/wintersun/p/4753145.html
Copyright © 2011-2022 走看看