【问题标题】:Sentry - adding arbitrary key/value pairs to the user contextSentry - 向用户上下文添加任意键/值对
【发布时间】:2019-09-18 15:08:36
【问题描述】:

在下面的documentation中,声明可以

...提供保留名称之外的任意键/值对,这些将与用户一起存储

在 C# 代码中,我有以下内容:

var _user = new {
  Login = "fred",
  EmailAddress = "fred@here.com",
  Name = "Fred Flintstone"
}

SentrySdk.ConfigureScope( scope => {
    scope.User = new Sentry.Protocol.User()
    {
        Id = _user.Login,
        Email = _user.EmailAddress,
        Username = _user.Login
    };
});

有没有办法添加Name(或任何其他字段)?还是文档只是指标签?

【问题讨论】:

  • 哦,我明白了。对于其他语言(如 Javascript),数据结构是动态的。有没有办法在 C# 中做到这一点?

标签: c# sentry


【解决方案1】:

您可以通过Other 属性添加您的自定义用户数据。

Sentry.Protocol 的最新版本将Other 作为IReadOnlyDictionary,这意味着您需要分配一个新实例,例如:

var sut = new User
{
    Id = "user-id",
    Email = "test@sentry.io",
    IpAddress = "::1",
    Username = "user-name",
    Other = new Dictionary<string, string>
                {
                    {"Name", "your name"},
                    {"anything else", "whatever"},
                }
};

This PR is making Other mutable 所以你可以添加如下数据:

var user = new User();
user.Other.Add("key", "value");

【讨论】:

    猜你喜欢
    • 2021-05-21
    • 2020-04-03
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多