• 使用log4net将C#日志发送到Elasticsearch


    一、安装Elasticsearch

      参考前面写的文章:https://www.cnblogs.com/songxingzhu/p/7909486.html

      安装完Elasticsearch后,修改/etc/elasticsearch/elasticsearch.yml文件,并追加下列内容。

    network.host: 0.0.0.0
    http.port: 9200

      然后重启Elasticsearch:

    systemctl restart elasticsearch

    二、新建C#项目,并引入Log4net库和log4net.ElasticSearch库。

      log4net:https://www.nuget.org/packages/log4net/

      log4net.ElasticSearch:https://www.nuget.org/packages/log4net.ElasticSearch/

    三、在项目中添加log4net.config文件,并设置成“如果较新则复制”,内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
        </appender>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="logs/" />
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <staticLogFileName value="false" />
          <datePattern value="yyyyMMdd'.log'" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="1MB" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
          </layout>
          <connectionString value="Server=139.219.141.155;Index=log_test;Port=9200;rolling=false"/>
          <lossy value="false" />
          <evaluator type="log4net.Core.LevelEvaluator">
            <threshold value="ALL" />
          </evaluator>
          <bufferSize value="1" />
        </appender>
        <root level="ALL">
          <appender-ref ref="ConsoleAppender" />
          <appender-ref ref="RollingLogFileAppender" />
          <appender-ref ref="ElasticSearchAppender" />
        </root>
      </log4net>
    </configuration>

      注意将黄色区域,更换成您安装好的Elasticsearch服务器的IP地址。

    四、Main函数中简单写日志输出

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Log4NetForELK
    {
        class Program
        {
            static void Main(string[] args)
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
                var repository = log4net.LogManager.GetLogger(typeof(Program));
                for (int i = 0; i < 100; i++)
                {
                    repository.InfoFormat("haha this is a test {0}", i);
                    repository.Warn("中文测试,柱柱同学。");
                    repository.Error("here is error.", new Exception("this is exception."));
                }
            }
        }
    }

    五、在Elasticsearch2平台端安装head并搜索日志

    /usr/share/elasticsearch/bin/plugin install file:/usr/share/elasticsearch/bin/elasticsearch-head-master.zip

    六、搜索日志

      打开浏览器,输入:http://139.219.141.155:9200/_plugin/head/

      

     

  • 相关阅读:
    Java语言中:float数据类型在内存中是怎么存储的?
    Java语言中:在数据类型的讲解中补充的几个小问题
    groovy : poi 导出 Excel
    php 基础入门篇之前言
    C 实现strcmp,strcpy,strcat函数
    CSDN 夏令营程序 试题分析 (3)
    HDU 1560 IDA*搜索
    iOS怎样找到自己的沙盒
    51系列小型操作系统精髓 简单实现2
    排序算法总结之折半插入排序
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/9255477.html
Copyright © 2020-2023  润新知