[英]Blazor wasm, net core hosted, with authentication, relationships between applicationuser and my models in the shared project
[英]Serilog in the blazor wasm hosted project
我是第一次嘗試 Serilog,我無法理解它在序列化過程中如何解析數據。 我正在使用 Serilog.Sinks.Http 將異常信息從客戶端(托管的 Blazor wazm)傳遞到服務器(ASP.NET Core 6.0)。
按照我的理解,Serilog.AspNetCore 只能在服務器端使用,我能夠成功配置它。 但是,在客戶端,我需要一個可以序列化為 HTTP 的庫。
所以我找到了 Serilog.Sinks.Http。 首先,我在 Serilog 注冊部分遇到了一個問題:我找不到在客戶端插入 Serilog 提供程序的方法,而我可以在服務器端插入。 這意味着在服務器端 Serilog 很好地集成到 MS Logging 基礎設施中,而在客戶端則不然。 我只能在 DI 容器中注冊它。 但它與 MS 日志框架完全脫節。 那么,兩個問題:
Q1) 是否可以在 blazor wasm 托管項目中將 Serilog 注冊為自定義提供程序,以便與 MS 框架集成? 這是我現在的代碼:
builder.Logging.Services.AddScoped<Serilog.ILogger>(_ =>
new LoggerConfiguration()
.WriteTo.Http(requestUri: uri)
.CreateLogger()
.ForContext<Program>());
Q2)如何控制序列化部分? 基本上我感興趣的是傳遞帶有一些附加數據的整個異常 object,或者傳遞我的自定義實體,然后在服務器端記錄該實體。 我知道我可以與正常的 web api 呼叫建立連接,但據我了解,Serilog 具有高級緩沖系統,它能夠存儲消息,然后在連接重新激活時重新發送它們,例如。
我試過這樣的事情:
Logger.Error( exception, someMessage);
然后我在服務器端得到一個奇怪的 JSON 結構,我不知道如何處理它。 這是我拋出 AccessViolationException 的示例。 我認識第一個 #events# 部分,我在 github 的一些示例中看到過它,他們在其中使用 LogEvents 進行映射:
public class LogEvents { public LogEvent[] Events { get; set; } = null!; }
但我就是無法使用我的自定義 object(更多數據):
{{
"events": [
{
"Timestamp": "2021-12-20T19:04:25.822+01:00",
"Level": "Error",
"MessageTemplate": "Exception while bla bla bla",
"RenderedMessage": "Exception while bla bla bla",
"Exception": "System.AccessViolationException: bla bla at bla bla",
"Properties": {
"SourceContext": "Program"
}
}
]
}}
ChildrenTokens: Count = 1
Count: 1
First: {"events": [
{
"Timestamp": "2021-12-20T19:04:25.822+01:00",
"Level": "Error",
"MessageTemplate": "Exception while bla bla bla",
"RenderedMessage": "Exception while bla bla bla",
"Exception": "System.AccessViolationException: bla bla at bla bla",
"Properties": {
"SourceContext": "Program"
}
}
]}
HasValues: true
Last: {"events": [
{
"Timestamp": "2021-12-20T19:04:25.822+01:00",
"Level": "Error",
"MessageTemplate": "Exception while bla bla bla",
"RenderedMessage": "Exception while bla bla bla",
"Exception": "System.AccessViolationException: bla bla at bla bla",
"Properties": {
"SourceContext": "Program"
}
}
]}
Next: null
Parent: null
Path: ""
Previous: null
Root: {{
"events": [
{
"Timestamp": "2021-12-20T19:04:25.822+01:00",
"Level": "Error",
"MessageTemplate": "Exception while bla bla bla",
"RenderedMessage": "Exception while bla bla bla",
"Exception": "System.AccessViolationException: bla bla at bla bla",
"Properties": {
"SourceContext": "Program"
}
}
]
}}
Type: Object
Results View: Expanding the Results View will enumerate the IEnumerable
Dynamic View: Expanding the Dynamic View will get the dynamic members for the object
為了獲得傳遞給服務器的 Blazor WASM 客戶端 Serilog 信息進行處理,我遵循了 https://nblumhardt.com/2019/11/serilog-blazor/ ,它使用 Serilog.Sinks.BrowserHttp 和 Serilog.AspNetCore.Ingestion,它們都是 pre -發布 Nuget 個軟件包。
根據對該鏈接的評論,我還發現有必要將 endpointUrl 參數提供為:
.WriteTo.BrowserHttp(endpointUrl: builder.HostEnvironment.BaseAddress+"ingest"
在客戶端Program.cs
var levelSwitch = new LoggingLevelSwitch();
string LogTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message,-120:j} {NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
.WriteTo.BrowserHttp(endpointUrl: builder.HostEnvironment.BaseAddress + "ingest", controlLevelSwitch: levelSwitch)
.WriteTo.BrowserConsole(outputTemplate: LogTemplate)
.CreateLogger();
在服務器 Program.cs
app.UseSerilogIngestion();
app.UseSerilogRequestLogging();
它適用於 .NET6。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.