.net-core - Serilog-将“严重性”属性添加到 GKE 的顶级 LogEvent?
问题描述
我Serilog.Formatting.Json.JsonFormatter
在 GKE 的 .NET Core 应用程序中使用 Serilog 和格式化程序。我正在登录到由 GKE Logging 代理读取的控制台。GKE 日志记录代理在日志事件的顶层需要一个“严重性”属性: GCP Cloud Logging LogEntry docs
因此,我的所有日志都显示在 GCP Logging 中,严重性为“Info”,因为 Serilog位于 GCP 中 LogEntryLevel
的jsonPayload
属性中。以下是 Cloud Logging 中的示例 LogEntry:
{
insertId: "1cu507tg3by7sr1"
jsonPayload: {
Properties: {
SpanId: "|a85df301-4585ee48ea1bc1d1."
ParentId: ""
ConnectionId: "0HM64G0TCF3RI"
RequestPath: "/health/live"
RequestId: "0HM64G0TCF3RI:00000001"
TraceId: "a85df301-4585ee48ea1bc1d1"
SourceContext: "CorrelationId.CorrelationIdMiddleware"
EventId: {2}
}
Level: "Information"
Timestamp: "2021-02-03T17:40:28.9343987+00:00"
MessageTemplate: "No correlation ID was found in the request headers"
}
resource: {2}
timestamp: "2021-02-03T17:40:28.934566174Z"
severity: "INFO"
labels: {3}
logName: "projects/ah-cxp-common-gke-np-946/logs/stdout"
receiveTimestamp: "2021-02-03T17:40:32.020942737Z"
}
我的第一个想法是使用 Enricher 添加“严重性”属性:
class SeverityEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddOrUpdateProperty(
propertyFactory.CreateProperty("Severity", LogEventLevel.Error));
}
}
生成的日志在 GCP 中如下所示,并且仍然标记为 Info:
{
insertId: "wqxvyhg43lbwf2"
jsonPayload: {
MessageTemplate: "test error!"
Level: "Error"
Properties: {
severity: "Error"
}
Timestamp: "2021-02-03T18:25:32.6238842+00:00"
}
resource: {2}
timestamp: "2021-02-03T18:25:32.623981268Z"
severity: "INFO"
labels: {3}
logName: "projects/ah-cxp-common-gke-np-946/logs/stdout"
receiveTimestamp: "2021-02-03T18:25:41.029632785Z"
}
Serilog 中是否有任何方法可以将“严重性”属性添加到与“jsonPayload”相同的级别而不是在其中?我怀疑 GCP 会选择它并适当地记录错误类型。
作为最后的手段,我可能会使用 GCP 日志接收器,但我当前的设置在 GKE 日志代理已经存在的情况下更加方便和高效。
这是一个相关的 Stack Overflow 帖子,没有我已经拥有的信息或建议,这不足以解决这个问题:https ://stackoverflow.com/questions/57215700
解决方案
我发现以下信息详细说明了每个 SeriLog 到 Stackdriver 日志级别的严重性,下表也可能对您有所帮助
Serilog | 堆栈驱动程序 |
---|---|
详细 | 调试 |
调试 | 调试 |
信息 | 信息 |
警告 | 警告 |
错误 | 错误 |
致命的 | 批判的 |
完整信息可在以下链接中找到
https://github.com/manigandham/serilog-sinks-googlecloudlogging#log-level-mapping
我认为这段代码可以帮助您让 Stackdriver 识别 SeriLogs 给出的日志的严重性。
private static LogSeverity TranslateSeverity(LogEventLevel level) => level switch
{
LogEventLevel.Verbose => LogSeverity.Debug,
LogEventLevel.Debug => LogSeverity.Debug,
LogEventLevel.Information => LogSeverity.Info,
LogEventLevel.Warning => LogSeverity.Warning,
LogEventLevel.Error => LogSeverity.Error,
LogEventLevel.Fatal => LogSeverity.Critical,
_ => LogSeverity.Default
};
我将在这里留下完整代码的链接
问候!
推荐阅读
- paypal - 安装模块 PayPal 后 Prestashop 损坏
- r - 在 R 中,我如何计算观察次数和它们出现的组数?
- c# - 页面仅在用户被选中时可见
- c# - 对不使用 MVVM 模式的 xamarin 表单项目进行单元测试
- python - SyntaxError: 'return' 外部函数错误
- python - 在python的循环中使用stdout和stdin导致错误
- mysql - 将 PHP 兼容的正则表达式转换为 MySql 兼容的正则表达式 - 提取澳大利亚手机号码
- mysql - 字段列表中的未知列“r”
- flutter - Flutter 导航如何移动到每个项目的第二页
- python - Pandas read_sql 列不正确