0

I want to use UTC DateTime in Serilog filename. I am using the below configuration for serilog in my appseting.json file.

"Serilog": {
    "Using": [],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId",
      "WithThreadId"
    ],
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log-.json",
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
          "rollingInterval": "Hour",
          "shared": true
        }
      }
    ]
  }

and below is the program.cs file code which I use for serilog.

public static void Main(string[] args)
        {

            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json").Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();
            try
            {
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception e)
            {
                Log.Fatal(e, "The Application failed to start correctly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args).UseSerilog()
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

Currently, serilog producing the log file name as log-2020091323.json which is not UTC DateTime. I'm using asp.net core 3.1.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • Does this answer your question? [Is it possible to get Timestamp in output template as DateTimeKind.Utc?](https://stackoverflow.com/questions/35369491/is-it-possible-to-get-timestamp-in-output-template-as-datetimekind-utc) – Ruben Bartelink Sep 14 '20 at 06:11
  • Thanks, @RubenBartelink. But I need the UTC date in the file name, not in the file content. It's maybe creating an extra property in file content call UtcTimestamp. – Md. Mustafizur Rahman Sep 14 '20 at 06:21
  • Sorry picked too quick - I think I saw one of those pre-asnwered in the search too? – Ruben Bartelink Sep 14 '20 at 06:30

1 Answers1

2

You can use Install-Package Serilog.Sinks.Map nuget package and configet it in Serilog setup to archive this as in below code example

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    // Ex: UtcDateTime value '202009201010'
    .WriteTo.Map("UtcDateTime", DateTime.UtcNow.ToString("yyyyMMddHHmm")
                    , (UtcDateTime, wt) => wt.File($"logs/log-{UtcDateTime}.txt"))
    .CreateLogger();

Log.Information("Hello, Serilog!");

Log.CloseAndFlush(); // Ex: log file name 'log-202009201010.txt'

This code save logs to new file each minute if you need to configure it for each hour use DateTime.UtcNow.ToString("yyyyMMddHH"), or DateTime.UtcNow.ToString("yyyyMMdd") for each day.

ElasticCode
  • 7,311
  • 2
  • 34
  • 45