如何在弹性搜索中每天使用Serilog自动生成索引

时间:2019-10-10 05:24:19

标签: c# serilog

我将serilog 2.8与dotnet框架4.8一起使用。相同的日志系统通过使用以下代码连接到elasticsearch。

    var isxyz = Matching.FromSource("xyz");
    var elasticUrl = ConfigurationManager.GetSetting("elasticurl", "http://localhost:9200"); 
    var environment= ConfigurationManager.GetSetting("environment", "test").ToLowerInvariant();
    string servicename = "john.processExecutor";
    var indexname = $"{servicename.ToLowerInvariant()}.{environment}.{DateTime.Now.ToString("yyyyMMdd")}";
    Log.Logger = new LoggerConfiguration()

    .Enrich.WithProperty("ServiceName", servicename)
    .Enrich.FromLogContext()
    .MinimumLevel.Debug()

    .WriteTo.Logger(l => l.WriteTo.Console())

       .WriteTo.Logger(l => l.Filter.ByIncludingOnly(m => isxyz(m))
        .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticUrl))
        {
            AutoRegisterTemplate = true,
            AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
             IndexFormat = indexname
        })
        .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "Logs" + "\\" + "Application-.txt", rollingInterval: RollingInterval.Day,
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
     )

    .WriteTo.Logger(l => l.Filter.ByIncludingOnly(m => isxyz(m)).WriteTo
    .File(AppDomain.CurrentDomain.BaseDirectory + "Logs" + "\\" + "log-.txt", rollingInterval: RollingInterval.Day,
    outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] [{FilePath}]  [{MemberName}] [{LineNumber}] [{ServiceName}] [{CountryCode}] [{Message:lj}{NewLine}{Exception}]"))

    .WriteTo.Logger(l => l.WriteTo
    .File(AppDomain.CurrentDomain.BaseDirectory + "Logs" + "\\" + "fulllog-.txt", rollingInterval: RollingInterval.Day,
     outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] [{FilePath}] [{MemberName}] [{LineNumber}] [{ServiceName}] [{CountryCode}] [{Message:lj}{NewLine}{Exception}]"))

    .CreateLogger();

我想每天自动生成一个索引。我尝试使用indexdecider。但没有获得实现此目标的方法。

1 个答案:

答案 0 :(得分:0)

您尝试过吗?

> "log-{0:yyyy.MM.dd}"