Log4Net 和 ElasticSearch - 多个索引
本文关键字:索引 ElasticSearch Log4Net | 更新日期: 2023-09-27 18:31:38
我正在使用带有ElasticsearchAppender的log4net。现在我想添加另一个 ElasticSearch 的追加器,但带有另一个索引。我现在做的是:
<appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date - %level - %message %property{label} %property{mstimeload} %property{applicationid} %property{page}
%property{ipclient} %property{browser} %property{browsersignature} %property{appversion} %property{sessionuniquecodetag} %property{globalcountertailsloaded}
%property{ipserveraddress}" />
</layout>
<connectionString value="Server=myip;Index=log;Port=9200;rolling=true"/>
<lossy value="true" />
<bufferSize value="100" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="INFO"/>
</evaluator>
</appender>
<appender name="ElasticSearchAppenderClient" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date - %level - %message %property{label} %property{applicationid} %property{ipclient} %property{browser} %property{browserversione} %property{browsersignature}"/>
</layout>
<connectionString value="Server=myip;Index=logclient;Port=9200;rolling=true"/>
<lossy value="true" />
<bufferSize value="100" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="INFO"/>
</evaluator>
</appender>
然后在服务器端我有
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("ElasticSearchAppender");
private static readonly log4net.ILog logclient = log4net.LogManager.GetLogger("ElasticSearchAppenderClient");
问题是当我添加属性的值时。 因为我所做的是例如:
log4net.ThreadContext.Properties["label"] = label;
log4net.ThreadContext.Properties["ipclient"] = ipclient;
log4net.ThreadContext.Properties["applicationid"] = applicationid;
log4net.ThreadContext.Properties["page"] = page;
...
log.Info(ex);
对于第一个日志和
log4net.ThreadContext.Properties["label"] = label;
log4net.ThreadContext.Properties["browser"] = browser;
log4net.ThreadContext.Properties["browserversion"] = browserversion;
...
logclient.Info("");
但是在带有索引logclient
的日志中,我发现索引的属性log
,反之亦然。相反,我希望有两个不同且独立的日志索引,每个索引具有不同的属性。
看起来您直接从您的应用程序登录到 Elasticsearch 到单个索引logclient
。
您可以创建两个具有单独索引的命名记录器,并在整个代码中适当地使用记录器。
您可能还需要考虑更改type
而不是索引。
此外,您可能还需要考虑使用Logstash。虽然它是您将负责的另一项服务,但它可以为表带来很多好处,例如将日志路由到索引和类型或 ip 到geo_hash转换。
最后,一年多前,我从Log4net切换到Logary,以改善与Logstash/Elasticsearch的集成,并且非常满意。
我想
我已经找到了解决方案。在web.config中,我做了这样的事情:
<log4net>
<appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date %level %c{1} %c{2}" />
</layout>
<connectionString value="Server=localhost;Index=log;Port=9200;rolling=true"/>
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ALL" />
</evaluator>
<bufferSize value="100" />
</appender>
<appender name="ElasticSearchAppenderClient" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date %level %c{1} %c{3}" />
</layout>
<connectionString value="Server=localhost;Index=logclient;Port=9200;rolling=true"/>
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ALL" />
</evaluator>
<bufferSize value="100" />
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="ElasticSearchAppender" />
</root>
<logger name="ElasticSearchAppenderClient" additivity="False"> <!-- additivity="False" VERY IMPORTANT!! -->
<level value="ALL" />
<appender-ref ref="ElasticSearchAppenderClient" />
</logger>
</log4net>
然后我为每个索引创建了两个不同的类,然后将这个类传递给我的日志:
private static readonly ILog _log = LogManager.GetLogger("ElasticSearchAppender");
private static readonly ILog _logclient = LogManager.GetLogger("ElasticSearchAppenderClient");
Log thislog = new Log ();
thislog.label = "log";
thislog.mstime = 1;
_log.Error(thislog);
LogClient thislogc = new LogClient();
thislogc.label = "logclient";
thislogc.browser = 2;
_logclient.Error(thislogc);