使用Laravel 5.1无法将user_context传递到Sentry

My goal is to pass User Context like email or ID into Sentry so I can see which users broke something.

I've configured a piece of Global Middleware to add user context to my Sentry errors. Here is the class:

        class AddUserToSentry
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if(Auth::user())
        {
            //dd(Auth::user()->email);
            app('sentry')->user_context(array(
                'email' => Auth::user()->email
            ));
        }
        return $next($request);
    }
}

In my Handler.php I have:

 /**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $e
 * @return void
 */
public function report(Exception $e)
{

    if ($this->shouldReport($e)) {
        app('sentry')->captureException($e);

    }
    parent::report($e);
}

What am I missing to make this work? All I get for user context is the IP address, which is not very helpful in my case.

Thank you very much,

Josh

You need to provide access to Laravel's Auth Facade in your controller like so:

Use Auth;

Here is a complete example, the source is from the official Sentry documentation. To avoid having to add use Auth; you can simply use the auth() helper function.

namespace App\Http\Middleware;

use Closure;

class SentryContext
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (app()->bound('sentry')) {
            /** @var \Raven_Client $sentry */
            $sentry = app('sentry');

            // Add user context
            if (auth()->check()) {
                $sentry->user_context(['id' => auth()->user()->id, 'email' => auth()->user()->email]);
            }

            // Add tags context
            // $sentry->tags_context(['foo' => 'bar']);
        }

        return $next($request);
    }
}