很多小步快跑的公司,开发人员多则3-4个,面对巨大业务压力,日连夜的赶着上线,快速试错,自然就没时间搭建一些基础设施,比如说logCenter,但初期
项目不稳定,BUG又多,每次都跑到生产去找日志,确实也不大方便,用elk或者用hadoop做日志中心,虽然都是没问题的,但基于成本和人手还是怎么简化怎么来,
本篇就来说说直接使用log4net的ElasticSearchAppender扩展直接将log写入到es中。
nuget下来的都是开箱可用,如果看源码的话可以在github上找一下log4net.ElasticSearch项目。。
nuget包下来之后,就可以配置config文件了,其实还是蛮简单的,大家可以根据自己的项目合理的配置里面的各项参数,为了方便大家理解,我在每个配置
项上加了详细的注释,大家可以仔细看看。
<span ><!--</span><span > ES地址 rolling=true:表示每天一个index(datamipcrm_log_2018.05.31) </span><span >--></span> <span ><</span><span >connectionString </span><span >value</span><span >="Scheme=http;Server=192.168.23.145;Index=datamiprm_log;Port=9200;rolling=true"</span><span >/></span> <span ><!--</span><span > 有损配置: 如果配置的buffer满了还没来的及刷新到es中,那么新来的log将会被丢弃。 </span><span >--></span> <span ><</span><span >lossy </span><span >value</span><span >="false"</span> <span >/></span> <span ><</span><span >evaluator </span><span >type</span><span >="log4net.Core.LevelEvaluator"</span><span >></span> <span ><!--</span><span > 表示 小于ERROR级别的日志会进入buffer数组,大于等于这个级别的,直接提交给es。 通常情况下,ERROR级别的错误,我们直接塞到ES中,这样更有利于我们发现问题。 DEBUG,INFO WARN ERROR </span><span >--></span> <span ><</span><span >threshold </span><span >value</span><span >="ERROR"</span> <span >/></span> <span ></</span><span >evaluator</span><span >></span> <span ><!--</span><span > buffer池的阈值50,一旦满了就会触发flush 到 es的动作(bulk api) </span><span >--></span> <span ><</span><span >bufferSize </span><span >value</span><span >="50"</span> <span >/></span> <span ></</span><span >appender</span><span >></span> <span ><</span><span >root</span><span >></span> <span ><!--</span><span > 指定所有的loglevel(DEBUG,INFO,WARN,ERROR)级别都是用 ElasticSearchAppender 处理 </span><span >--></span> <span ><</span><span >level </span><span >value</span><span >="ALL"</span><span >/></span> <span ><</span><span >appender-ref </span><span >ref</span><span >="ElasticSearchAppender"</span> <span >/></span> <span ></</span><span >root</span><span >></span><span ></</span><span >log4net</span><span >></span><span ><</span><span >startup</span><span >></span> <span ><</span><span >supportedRuntime </span><span >version</span><span >="v4.0"</span><span > sku</span><span >=".NETFramework,Version=v4.6.1"</span> <span >/></span><span ></</span><span >startup</span><span >></span>
<span ></
<span >configuration<span >>上面有几点要特别注意一下:
这个大家理解成缓存区,方便批量提交到es中,否则的话,过于频繁的和es进行交互,对带宽,对application,es都是比较大的压力。
有时候我们有这样的需求,我希望ERROR,Fault这种级别的错误不要走buffer,直接提交给es,这样更容易让初创团队发现问题,找到问题,恭喜你,
上面这个配置就是解决这个事的。
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号// 方法是按如下所示使用“*”: :// [assembly: AssemblyVersion("1.0.*")][assembly: AssemblyVersion("1.0.0.0")][assembly: AssemblyFileVersion("1.0.0.0")]
ILog _log = LogManager.GetLogger(
</span><span >static</span> <span >void</span> Main(<span >string</span><span >[] args) {</span><span > for</span> (<span >int</span> i = <span >0</span>; i < <span >1000</span>; i++<span >) { </span><span >try</span><span > { </span><span >var</span> m = <span >"</span><span >1</span><span >"</span><span >; </span><span >var</span> result = <span >100</span> /<span > Convert.ToInt32(m); _log.Info(</span><span >"</span><span >我要开始记录日志啦</span><span >"</span><span >); } </span><span >catch</span><span > (Exception ex) { _log.Error(</span><span >"</span><span >调用失败</span><span >"</span> +<span > i,ex); </span><span >//</span><span >_log.Info("调用失败" + i,ex); </span><span >//</span><span >Console.WriteLine(i);</span>
<span > }
}
Console.Read(); }}
}
然后通过elasticsearch-head 插件进行查看,各种数据就都在es中了。
关于这个插件的下载,因为google官网被屏蔽,大家如果有VPN的话,可以自行在chrome商店搜索,当然也可以直接下载我的zip包,。
在浏览器中加载插件的时候选择0.1.3.0 文件夹即可
完了之后点击右上边的 “放大镜“ 按钮就可以看到你想看到的UI了。
这是一个基于luncene的分布式搜索框架,用起来还是挺顺手的,你可以下载5.6.4版本。
[elsearch@localhost myapp]$ -...gz一般来说,安装的过程中你可能会遇到3个坑。
[root@localhost bin]# ./--31T04::,][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.) ~[elasticsearch-..jar:.
这个简单,增加一个elasearch用户就可以了。
-g elsearch - -R elsearch:elsearch ./elasticsearch #指定elasticsearch所属elsearch组[elsearch@localhost bin]$ ./elasticsearchJava HotSpot(TM) 64-Bit Server VM warning: INFO: ## There is insufficIEnt memory for the Java Runtime Environment to continue.# Native memory allocation (mmap) Failed to map 1449590784 bytes for committing reserved memory.# An error report file with more information is saved as:# /usr/myapp/elasticsearch/bin/hs_err_pID33341
这是ES默认分配的堆内存是2g,如果出现这个问题,一般来说是你的虚拟机拥有的内存小于2g,只需要在jvm.options中将2g修改1g就可以了。
[root@localhost config]# <span >-xms1g-Xmx1g
[elsearch@localhost bin]$ ./elasticsearch[2018-05-30T20:44:56,484][INFO ][o.e.n.Node ] [] initializing ...[2018-05-30T20:44:56,632][INFO ][o.e.e.nodeenvironment ] [f9t2Sfl] using [1] data paths,mounts [[/ (rootfs)]],net usable_space [14.4gb],net total_space [22.1gb],spins? [unkNown],types [rootfs][2018-05-30T20:44:56,632][INFO ][o.e.e.nodeenvironment ] [f9t2Sfl] heap size [989.8mb],compressed ordinary object pointers [true][2018-05-30T20:44:56,634][INFO ][o.e.n.Node ] node name [f9t2Sfl] derived from node ID [f9t2SfljReiND4XeMLUbyA]; set [node.name] to overrIDe[2018-05-30T20:44:56,634][INFO ][o.e.n.Node ] version[5.6.4],pID[33546],build[8bbedf5/2017-10-31T18:55:38.105Z],OS[linux/3.10.0-327.el7.x86_64/amd64],JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_144/25.144-b01][2018-05-30T20:44:56,634][INFO ][o.e.n.Node ] JVM arguments [-xms1g,-Xmx1g,-XX:+UseConcmarkSweepGC,-XX:CMSInitiatingOccupancyFraction=75,-XX:+UseCMSInitiatingOccupancyOnly,-XX:+AlwaysPretouch,-Xss1m,-Djava.awt.headless=true,-Dfile.enCoding=UTF-8,-Djna.nosys=true,-Djdk.io.permissionsUseCanonicalPath=true,-dio.netty.noUnsafe=true,-dio.netty.noKeySetoptimization=true,-dio.netty.recycler.maxCapacityPerThread=0,-Dlog4j.shutdownHookEnabled=false,-Dlog4j2.disable.jmx=true,-Dlog4j.skipJansi=true,-XX:+HeapDumpOnOutOfMemoryError,-Des.path.home=/usr/myapp/elasticsearch][2018-05-30T20:44:57,530][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [aggs-matrix-stats][2018-05-30T20:44:57,530][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [ingest-common][2018-05-30T20:44:57,530][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [lang-Expression][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [lang-groovy][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [lang-mustache][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [lang-painless][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [parent-join][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [percolator][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [reindex][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [transport-netty3][2018-05-30T20:44:57,531][INFO ][o.e.p.PluginsService ] [f9t2Sfl] loaded module [transport-netty4][2018-05-30T20:44:57,532][INFO ][o.e.p.PluginsService ] [f9t2Sfl] no plugins loaded[2018-05-30T20:44:59,469][INFO ][o.e.d.discoveryModule ] [f9t2Sfl] using discovery type [Zen][2018-05-30T20:45:00,107][INFO ][o.e.n.Node ] initialized[2018-05-30T20:45:00,107][INFO ][o.e.n.Node ] [f9t2Sfl] starting ...[2018-05-30T20:45:00,339][INFO ][o.e.t.TransportService ] [f9t2Sfl] publish_address {192.168.23.143:9300},bound_addresses {[::]:9300}[2018-05-30T20:45:00,356][INFO ][o.e.b.bootstrapChecks ] [f9t2Sfl] bound or publishing to a non-loopback or non-link-local address,enforcing bootstrap checksERROR: [2] bootstrap checks Failed[2018-05-30T20:45:00,365][INFO ][o.e.n.Node ] [f9t2Sfl] stopPing ...[2018-05-30T20:45:00,444][INFO ][o.e.n.Node ] [f9t2Sfl] stopped[2018-05-30T20:45:00,444][INFO ][o.e.n.Node ] [f9t2Sfl] closing ...[2018-05-30T20:45:00,455][INFO ][o.e.n.Node ] [f9t2Sfl] closed
这个max file descriptors 的问题,我只需要修改 vim /etc/security/limits.conf 文件增加句柄值即可。
虚拟内存的 max virtual memory areas vm.max_map_count [65530] is too low,increase to at least [262144] 只需要在sysctl.conf 中修改max即可。
[root@localhost config]# vim /etc/vm.max_map_count=<span >655360这些坑解决的话,es就可以正常启动了,最后记得在elasticsearch.yml 中将host设为0.0.0.0 让远程机器可以访问。
network.host: <span >0.0.<span >0.0最后执行 ./elasticsearch -d 让es在后台执行。
[elsearch@localhost bin]$ ./elasticsearch -d[elsearch@localhost bin]$ netstat -tlnp
(Not all processes Could be IDentifIEd,non-owned process info
will not be shown,you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.122.1:53 0.0.0.0: ListEN -
tcp 0 0 0.0.0.0:22 0.0.0.0: ListEN -
tcp 0 0 127.0.0.1:631 0.0.0.0: ListEN -
tcp 0 0 127.0.0.1:25 0.0.0.0: ListEN -
tcp6 0 0 :::9200 ::: ListEN 17523/java
tcp6 0 0 :::9300 ::: ListEN 17523/java
tcp6 0 0 :::22 ::: ListEN -
tcp6 0 0 ::1:631 ::: ListEN -
tcp6 0 0 ::1:25 :::* ListEN -
[elsearch@localhost bin]$
好了,本篇就说这么多,如果你要更精细化的查询,可以再搭建一个配套版本的kibana即可。
总结以上是内存溢出为你收集整理的小步快跑的公司可以最简化 *** 作直接通过log4net将日志写入ElasticSearch全部内容,希望文章能够帮你解决小步快跑的公司可以最简化 *** 作直接通过log4net将日志写入ElasticSearch所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)