This error occurred on an upgrade from Drupal 9 to Drupal 10

 The "site.path" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the site.path parameter instead

Solution

Change from

\Drupal::service('site.path'))

to:

\Drupal::getContainer()->getParameter('site.path')