【问题标题】:Where to add context to Sentry-php?在哪里向 Sentry-php 添加上下文?
【发布时间】:2021-05-21 17:10:04
【问题描述】:

正如 Sentry Docs 中所解释的,我正在尝试向我的 PHP 应用程序添加一些上下文: https://docs.sentry.io/platforms/php/enriching-events/context/

我已将 configureScope 置于异常中,但由于我只在没有上下文的情况下收到错误,所以我问自己它是否是设置它的正确位置。我进行了搜索,发现的所有信息都是关于特定框架的。

注意:在你问之前,会话数据不为空。

<?php
require '../vendor/autoload.php';

Sentry\init([
    'dsn'         => 'https://.......<secret>.........',
    'environment' => Environment::get(),
]);
try {
    new Application();
} catch (\Throwable $exception) {
    Sentry\configureScope(function (Sentry\State\Scope $scope): void {
        $scope->setExtra('email', Session::get('user_email'));
        $scope->setExtra('cart', Session::get('cart'));
        $scope->setExtra('token', Session::get('token'));
    });
    Sentry\captureException($exception);
}

【问题讨论】:

  • 您好,我已经尝试了您的确切代码,它运行良好,您没有提及您使用的 SDK 版本,如果您使用的是自托管版本或只是哨兵,也没有提及.io,这可能很重要,但通常您显示的代码有效。确保您滚动到 Sentry UI 中的“附加数据”部分并且没有丢失它:)

标签: php sentry


【解决方案1】:

试试看这里:https://docs.sentry.io/platforms/php/enriching-events/context/

例如,您可以使用上下文捕获异常

\Sentry\withScope(function (\Sentry\State\Scope $scope) use ($e): void {
  $scope->setContext('character', [
    'name' => 'Mighty Fighter',
    'age' => 19,
    'attack_type' => 'melee'
  ]);

  \Sentry\captureException($e);
});

或为所有事件全局添加上下文

\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
  $scope->setContext('character', [
    'name' => 'Mighty Fighter',
    'age' => 19,
    'attack_type' => 'melee'
  ]);
});

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多