app-insights-php/monolog-handler

此包已被废弃且不再维护。未建议替代包。

Microsoft App Insights monolog处理器

0.3.1 2023-03-28 10:36 UTC

README

它为AppInsights提供了monolog处理器:用于跟踪依赖(AppInsightsDependencyHandler)和跟踪(AppInsightsTraceHandler)。

Tests

长时间运行进程

对于长时间运行的进程(例如:消费者),可能难以找到一个合适的时间将所有内容刷新到AppInsights。一种可能的解决方案是使用缓冲处理器。您可以使用缓冲处理器轻松地包装这两个处理器并设置一个溢出缓冲区。它们设计用于缓冲区溢出时将所有内容刷新到AppInsight。

限制

遥测数据的大小限制为64千字节。处理器在将其添加到AppInsights客户端队列之前检查遥测数据的长度。如果超出限制,则不会添加!这意味着AppInsights不应成为您数据的唯一来源。您应该在不受此类限制的其他地方保留数据副本。

Laravel中的使用示例

  1. 更新config/logging.php中的新条目
'appinsights' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => \AppInsightsPHP\Monolog\Handler\AppInsightsTraceHandler::class,
            'formatter' => \AppInsightsPHP\Monolog\Formatter\ContextFlatterFormatter::class
        ],
  1. 注册新的服务提供者
php artisan make:provider AppInsightsLogProvider

2.1 在boot()方法中注册日志处理器的服务提供者依赖项。

/**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->bind(
            AppInsightsTraceHandler::class,
            function ($app) {
                $telemetryClient = new \ApplicationInsights\Telemetry_Client();
                $context = $telemetryClient->getContext();
                $context->setInstrumentationKey(env('APPINSIGHTS_INSTRUMENTATIONKEY'));

                /** @var CacheManager $cacheManager */
                $failureCache = new FailureCache(Cache::repository(new NullStore()));

                /** @var Logger $defaultLogger */
                $defaultLogger = Log::getFacadeRoot();

                $client = new Client($telemetryClient, Configuration::createDefault(), $failureCache, $defaultLogger);

                $handler = new AppInsightsTraceHandler($client);

                return $handler;
            }
        );