How to change/override FilePath when creating logger using Serilog

Viewed 224

I want to change/override the filename declared appsettings.json with whatever I provide when I setup the logger while keeping all the other settings as is.

I have serilog settings declared in the appsettings.json

"Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "System": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path":"MyAppName_.log"
          "rollOnFileSizeLimit": "true",
          "fileSizeLimitBytes": 1000000,
          "rollingInterval": "Day",
          "flushToDiskInterval": 1,
          "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message:lj}{NewLine}{Exception}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "%COMPUTERNAME%", "WithThreadId", "USERNAME","USERDOMAIN" ],
    "Properties": { "Application": "MyAppName" }
  }

The below code is in ConfigureServices method in Startup.cs

var appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
var fileName = @$"C://Logs/{MyAppName}\{MyAppName}-{DateTime.Today.ToString("MM-dd-yyyy")}.log";
Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .WriteTo.File(fileName)
            .CreateLogger();

Is it possible?

I am using ASP.NET Core 3.1 and Serilog.AspNetCore 3.2.0

0 Answers
Related