繁体   English   中英

blazor wasm 托管项目中的 Serilog

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM