Flutter Azure Application Insights 监控插件dio_azure_application_insights_interceptor的使用
Flutter Azure Application Insights 监控插件 dio_azure_application_insights_interceptor
的使用
Dio
拦截器,用于将请求指标发送到 Azure Application Insights。
此包受先前为 Firebase 实现的启发。
使用方法
final dio = Dio();
final insightsInterceptor = DioAzureApplicationInsightsInterceptor();
dio.interceptors.add(insightsInterceptor);
额外信息
如果在 Azure 上部署且已设置与 Application Insights 的连接,拦截器会自动配置自己。自动配置通过读取环境变量 APPLICATIONINSIGHTS_CONNECTION_STRING
来完成。
或者,可以提供连接字符串或现有的 TelemetryClient
实例。
如果没有提供参数且环境变量不存在,则观察者不会提交任何日志(但也不会产生任何错误)。
注意:拦截器可选地支持注入自定义 HTTP 客户端以提交遥测数据。不要 使用被观测的 Dio 实例作为遥测的 HTTP 客户端,因为这会导致每个遥测项再次生成额外的遥测并因此使日志泛滥。
示例代码
以下是完整的示例代码:
import 'package:dio/dio.dart';
import 'package:dio_azure_application_insights_interceptor/dio_azure_application_insights_interceptor.dart';
void main() {
// 初始化 Dio 实例
final dio = Dio();
// 创建 DioAzureApplicationInsightsInterceptor 实例
final insightsInterceptor = DioAzureApplicationInsightsInterceptor();
// 将拦截器添加到 Dio 实例中
dio.interceptors.add(insightsInterceptor);
// 发送一个请求
dio.get('https://jsonplaceholder.typicode.com/posts')
.then((response) {
print('Response: ${response.data}');
})
.catchError((error) {
print('Error: $error');
});
}
运行示例代码
确保你已经在你的项目中添加了 dio
和 dio_azure_application_insights_interceptor
依赖,并正确设置了 APPLICATIONINSIGHTS_CONNECTION_STRING
环境变量。
-
在
pubspec.yaml
文件中添加依赖:dependencies: dio: ^4.0.0 dio_azure_application_insights_interceptor: ^1.0.0
-
运行
flutter pub get
命令以获取依赖。 -
设置环境变量
APPLICATIONINSIGHTS_CONNECTION_STRING
,例如在 macOS 或 Linux 中可以在.env
文件中设置:export APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=your-instrumentation-key-here"
更多关于Flutter Azure Application Insights 监控插件dio_azure_application_insights_interceptor的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html