本文介绍了索引格式每天至每周更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在将日志从Nlog发送到ElasticSearch.我每天创建索引,并将日志发送到该索引.我想每周创建索引,所以我想更改配置文件.

I am currently sending my Log from Nlog to ElasticSearch. I am creating Index daily, and send Logs to that index. I want to create Index Weekly, so I want to change config file.

我在NLog配置文件中创建索引. index = "logstash-${date:format=yyyy.MM.dd}"

I do index creation in NLog configuration file. index = "logstash-${date:format=yyyy.MM.dd}"

我的NLog配置部分:

My NLog Configuration part :

  <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}"

I found in some forums (https://github.com/logstash-plugins/logstash-output-elasticsearch/issues/541#issuecomment-270923437) changing weekly I should use like xxxx.ww.I tried to change config file like this : index = "logstash-${date:format=xxxx.ww}"

不幸的是,这是给我结果logstash-xxxx.ww,我期望的结果是logstash-2019.25

Unfortunately this is give me result logstash-xxxx.ww, I expected result logstash-2019.25

那么我该如何每天更改为每周一次?

So how can I change daily to weekly?

推荐答案

${date}接受与DateTime.ToString相同的格式.不幸的是,.NET没有ww或weeknumber格式(请参见自定义日期和时间格式的字符串-.NET | Microsoft文档)

${date} accepts the same formats as DateTime.ToString. Unfortunately there is no ww or weeknumber format with .NET (see Custom date and time format strings - .NET | Microsoft Docs)

论坛上的链接正在谈论Joda Time,它是Java而不是.NET的库.

The link on the forum is talking about Joda Time, which is a library for Java and not .NET.

您可以使用NLog中的自定义布局渲染器解决此问题.在.NET中获取星期数有些棘手,请从获取给定日期的正确星期数:

You could solve this with a custom layout renderer in NLog. Getting the week number in .NET is a bit tricky, got this from Get the correct week number of a given date:

// 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文档-如何编写自定义布局渲染器)

Creating a layout renderer which renders 2019.25 etc (see NLog docs - How to write a custom layout renderer)

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}"

这篇关于索引格式每天至每周更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:39