我当前正在将日志从Nlog发送到ElasticSearch。我每天创建索引,并将日志发送到该索引。我想创建每周索引,所以我想更改配置文件。
我在NLog配置文件中创建索引。 index = "logstash-${date:format=yyyy.MM.dd}"
index = "logstash-${date:format=yyyy.MM.dd}"
我的NLog配置部分:
<target xsi:type="ElasticSearch" index = "logstash-${date:format=yyyy.MM.dd}" uri="http://localhost:9200" includeAllProperties ="true"> </target>
我在一些论坛(https://github.com/logstash-plugins/logstash-output- elasticsearch/issues/541#issuecomment-270923437)中发现,我每周都应该使用xxxx.ww之类的内容。我试图像这样更改配置文件: index = "logstash-${date:format=xxxx.ww}"
index = "logstash-${date:format=xxxx.ww}"
不幸的是,这是给我的结果logstash-xxxx.ww,我是预期的结果logstash-2019.25
logstash-xxxx.ww
logstash-2019.25
那么我如何才能每天更改为每周一次?
${date}接受与相同的格式DateTime.ToString。不幸的是,.NET没有ww或weeknumber格式(请参阅自定义日期和时间格式字符串 -.NET | Microsoft Docs)
${date}
DateTime.ToString
论坛上的链接谈论的是Joda Time,它是Java而不是.NET的库。
// This presumes that weeks start with Monday. // Week 1 is the 1st week of the year with a Thursday in it. public static int GetIso8601WeekOfYear(DateTime time) { // Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll // be the same week# as whatever Thursday, Friday or Saturday are, // and we always get those right DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time); if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) { time = time.AddDays(3); } // Return the week of our adjusted day return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); }
创建一个渲染2019.25等的布局渲染器(请参阅NLog文档- 如何编写自定义布局渲染器)
using NLog.LayoutRenderers; using NLog; ... // register ${myDateTime}. Register a soon as possible (e.g main(), app_start etc) LayoutRenderer.Register("myDateTime", logEventInfo => logEventInfo.TimeStamp.Year +"." + GetIso8601WeekOfYear(logEventInfo.TimeStamp));
现在这应该工作:
index = "logstash-${myDateTime}"