欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

使用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/

  

 

 

 

posted @ 2018-07-02 19:47  宋兴柱  阅读(3765)  评论(3编辑  收藏  举报