Search code examples
c#serilog

How to generate Index automatically per day using Serilog in elastic search


Iam using serilog 2.8 with dotnet framework 4.8. The same log system is connecting to elasticsearch by using the below code.

    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();

I want to generate an index per day automatically. I tried to use indexdecider. But not getting how to achieve this.


Solution

  • have you tried?

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