Home > Net >  How do I get the value of a setting in appsettings.json from configuration in programs.cs Blazor app
How do I get the value of a setting in appsettings.json from configuration in programs.cs Blazor app

Time:01-07

Can anyone tell me how to get the value from an appsettings.json file in my program.cs file in a Blazor (core 5) App. Basically just need to write the "var seqUrl =" correctly. I think. Please and thank you.

In my Main method of Program.cs I have

IConfiguration configuration = new ConfigurationBuilder()                
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile("appsettings.Development.json", true, true)
            .Build();
        var levelSwitch = new LoggingLevelSwitch();
        var seqUrl = configuration.GetSection("SeriLog").GetSection("WriteTo").GetSection("Name:Seq").GetSection("Args").GetSection("serverUrl").Value;
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .MinimumLevel.ControlledBy(levelSwitch)
            .WriteTo.Seq(seqUrl, 
            apiKey: "xxxxxxxxxxxx",
            controlLevelSwitch: levelSwitch)
            .CreateLogger();

My appsettings.json looks like this.

"SeriLog": {
"Using": [
  "Serilog.Sinks.File",
  "Serilog.Sinks.Seq",
  "Serilog.Sinks.Console"
],
"MinimumLevel": {
  "Default": "Information",
  "Override": {
    "Microsoft": "Warning",
    "system": "Warning"
  }
},
"Enrich": [ "WithMachineName", "WithEnvironmentUserName", "WithClientIp", "WithClientAgent", "WithEnvironmentName", "WithProcessId", "WithProcessName", "WithThreadId" ],

"WriteTo": [ 
  {
    "Name": "File",
    "Args": {
      "path": "C:\\Temp\\Logs\\JsonLog.json",
      "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
      "rollingInterval": "Day"
    }
  },
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://xxxxxxxxxxx" 
    }
  }     
]

CodePudding user response:

I think you should consider restructuring the JSON in your appsettings file. The value for "WriteTo" is an array, but I would make it just an object rather than an array, then each element should be a child object. Use the "Name" variable to name them. Like this:

"WriteTo": { 
    "File": {
        "Args": {
        "path": "C:\\Temp\\Logs\\JsonLog.json",
        "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
        "rollingInterval": "Day"
        }
    },
    "Seq": {
        "Args": {
        "serverUrl": "http://xxxxxxxxxxx" 
        }
    }     
}

Then you should be able to retrieve the values using the same style that you are, just with the complete path to the variable you need. For example:

var seqUrl = configuration.GetSection("SeriLog:WriteTo:Seq:serverUrl").Value; I don't remember if this syntax is 100% correct or not (the path might not be colon separated, I'm not completely sure) but this concept should work. You just weren't entering the correct names into the .GetSection() method in your code, and I think it gets kind of tricky when you are trying to retrieve an array element like this, hence my suggestion to convert from array to a simple object.

  •  Tags:  
  • Related