admin管理员组文章数量:1429826
I need to pass UserId
as part of Serilog logging. For this I created a new variable UserId
with empty value in app settings, and set the value programmatically in index.razor
.
This code shown here is not working. How do I set the value for userId
in appsettings.json
?
Appsettings.json
:
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Seq",
"Args": { "serverUrl": "; }
}
],
"Properties": {
"ApplicationName": "TESTApp",
"Environment": "DEV"
"UserId": ""
}
}
index.razor
:
protected override async Task OnInitializedAsync()
{
UserName = GetUserId();
Configuration["Serilog:Properties:UserId"] = UserName;
}
I need to pass UserId
as part of Serilog logging. For this I created a new variable UserId
with empty value in app settings, and set the value programmatically in index.razor
.
This code shown here is not working. How do I set the value for userId
in appsettings.json
?
Appsettings.json
:
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Seq",
"Args": { "serverUrl": "https://logging.mysite" }
}
],
"Properties": {
"ApplicationName": "TESTApp",
"Environment": "DEV"
"UserId": ""
}
}
index.razor
:
protected override async Task OnInitializedAsync()
{
UserName = GetUserId();
Configuration["Serilog:Properties:UserId"] = UserName;
}
Share
Improve this question
edited Nov 21, 2024 at 4:48
marc_s
757k184 gold badges1.4k silver badges1.5k bronze badges
asked Nov 21, 2024 at 0:17
HenryHenry
8836 gold badges25 silver badges53 bronze badges
1
- Don't try to change the configuration, inject any extra context like stackoverflow/questions/51261177/add-username-into-serilog – Jeremy Lakeman Commented Nov 21, 2024 at 2:18
1 Answer
Reset to default 1I think you want changing the json file. You could try use "JsonObject" for easily modify the json text.
@using System.Text.Json
@using System.Text.Json.Nodes
var filePath = "appsettings.json";
var oldjson = File.ReadAllText(filePath);
var jsonobject = JsonObject.Parse(oldjson);
jsonobject["Serilog"]["Properties"]["UserId"] = UserName;
var newjson = jsonobject.ToString();
File.WriteAllText(filePath, newjson);
After the file has changed , builder.Configuration
will change automatically. Or you could make sure the configuration is reloaded when file change.
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
本文标签: cHow to set app settings value programmatically in Blazor serversideStack Overflow
版权声明:本文标题:c# - How to set app settings value programmatically in Blazor server-side - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745547033a2662751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论