【问题标题】:Sentry SDK dotnet core not using IUserFactorySentry SDK dotnet 核心不使用 IUserFactory
【发布时间】:2020-03-10 11:44:37
【问题描述】:

我正在尝试自定义我们向用户显示哨兵事件的方式。

我的appsettings是这样的:

"Sentry": {
    "DSN": "...",
    "IncludeRequestPayload": true,
    "SendDefaultPii": true,
    "MinimumBreadcrumbLevel": "Debug",
    "MinimumEventLevel": "Warning",
    "AttachStackTrace": true,
    "Debug": true,
    "DiagnosticsLevel": "Error"
  },

我将工厂注册为单例(我也尝试过瞬态) services.AddSingleton<Sentry.AspNetCore.IUserFactory, MySentryUserFactory>();

在我的启动中,我这样调用 UseSentry:

public static void Main(string[] args)
{
  WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) => ...)
        .UseKestrel(options => ...)
        .UseStartup<Startup>()
        .UseSentry()
        .Build().Run();
}

在 MySentryUserFactory 中,我没有看到它的效果,所以我在最顶部放置了一个 Console.WriteLine:

public User Create(HttpContext context)
{
  Console.WriteLine("------------------- I am using the custom userfactory");
  ...
}

所以我通过手动调用 SentrySdk 来测试它:

Exception e = new Exception("blah");
SentrySdk.CaptureException(e);

但是当我运行它时,我在控制台中看不到打印输出语句。然后,当我进入哨兵仪表板时,我看到了我正在测试的错误事件,并且没有显示用户(没有执行 IUserFactory 设置的操作)。

【问题讨论】:

    标签: c# .net-core sentry


    【解决方案1】:

    您需要根据您所做的配置选择加入以捕获用户身份信息。

    工作示例是(假设 DSN 可用:如 appsettings.json 或环境变量):

    public class Program : IUserFactory
    {
        public static void Main(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureServices(s => s.AddSingleton<IUserFactory, Program>())
                .UseSentry(o => o.SendDefaultPii = true)
                .Configure(a => a.Use( (context, next) => throw null))
                .Build()
                .Run();
    
        public User Create(HttpContext context) => new User {Id = "works"};
    }
    

    reviewing the code in Sentry's .NET SDK:

    if (options?.SendDefaultPii == true && !scope.HasUser())
    {
        var userFactory = context.RequestServices?.GetService<IUserFactory>();
        if (userFactory != null)
        {
            scope.User = userFactory.Create(context);
        }
    }
    

    我意识到您可能已经在范围内设置了一个用户,因此工厂没有被解析和调用?

    您可以通过回调来验证:

    .UseSentry(o =>
    {
        o.BeforeSend = @event =>
        {
            if (@event.HasUser())
            {
                // ...
                @event.User = new User();
            }
    
            return @event;
        };
    })
    

    或者使用可以向 DI 注册并获取依赖项的事件处理器:

    public class UserEventProcessor : ISentryEventProcessor
    {
        private readonly IUserFactory _userFactory;
        private readonly IHttpContextAccessor _accessor;
    
        public UserEventProcessor(IUserFactory userFactory, IHttpContextAccessor accessor)
        {
            _userFactory = userFactory;
            _accessor = accessor;
        }
    
        public SentryEvent Process(SentryEvent @event)
        {
            @event.User = _userFactory.Create(_accessor.HttpContext);
            return @event;
        }
    }
    
    // and register:
    services.ConfigureServices(s => s.AddScoped<ISentryEventProcessor, UserEventProcessor>())
    
    

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      相关资源
      最近更新 更多