实例介绍
OpenTracing为.NET Core应用提供了链路追踪支持。这一开源项目使得任何兼容OpenTracing的追踪器都能用于基于.NET Core的应用程序。
重要提示:OpenTracing和OpenCensus已合并形成OpenTelemetry!OpenTelemetry .NET库的信息请参考其官方网站。
支持的.NET版本
该项目目前仅支持面向.NET Core 3.1、.NET 6.0或.NET 7.0的应用。不支持完整的.NET框架,因为它使用了不同的追踪代码。
支持的库和框架
基于DiagnosticSource的追踪
该项目支持任何使用.NET的DiagnosticSource来进行代码追踪的库或框架。它将为每个Activity创建一个跨度,并为所有其他诊断事件创建span.Log调用。
为了进一步改善追踪输出,该库为以下库/框架提供了增强的追踪(Inject/Extract、标签、配置选项):
- ASP.NET Core
- Entity Framework Core
- System.Net.Http (HttpClient)
- System.Data.SqlClient
- Microsoft.Data.SqlClient
基于Microsoft.Extensions.Logging的追踪
该项目还将自己添加为Microsoft.Extensions.Logging系统中的日志提供者。它将为每个日志事件创建span.Log调用,但只有在有活动跨度(ITracer.ActiveSpan)时才会创建。
使用方法
该项目依赖于Microsoft的Microsoft.Extensions.*堆栈(例如,依赖注入,日志记录)中的几个包,因此其主要用途是ASP.NET Core应用和任何其他基于Microsoft.Extensions的控制台应用。
1. 将NuGet包OpenTracing.Contrib.NetCore添加到您的项目中。
2. 通过services.AddOpenTracing()将OpenTracing服务添加到IServiceCollection中。
在ASP.NET Core应用中,您可以将此调用添加到Program.cs文件的ConfigureServices方法中:
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureServices(services =>
{
// 启用并自动开始追踪!
services.AddOpenTracing();
})
.Build();
}
3. 确保启动InstrumentationService,它实现了IHostedService。InstrumentationService负责启动和停止追踪。
【实例截图】
【核心代码】
文件清单
└── csharp-netcore-f64af79ae582cebf52dcdb54797d2545a2bb0565
├── benchmarks
│ └── OpenTracing.Contrib.NetCore.Benchmarks
│ ├── AspNetCore
│ │ └── RequestDiagnosticsBenchmark.cs
│ ├── CoreFx
│ │ └── HttpHandlerDiagnosticsBenchmark.cs
│ ├── InstrumentationMode.cs
│ ├── OpenTracingBuilderExtensions.cs
│ ├── OpenTracing.Contrib.NetCore.Benchmarks.csproj
│ └── Program.cs
├── build.ps1
├── Directory.Build.props
├── global.json
├── launch-sample.ps1
├── LICENSE
├── NuGet.config
├── OpenTracing.Contrib.sln
├── package-icon.png
├── README.md
├── RELEASE.md
├── samples
│ ├── net6.0
│ │ ├── CustomersApi
│ │ │ ├── appsettings.json
│ │ │ ├── Controllers
│ │ │ │ └── CustomersController.cs
│ │ │ ├── CustomersApi.csproj
│ │ │ ├── DataStore
│ │ │ │ └── CustomerDbContext.cs
│ │ │ ├── Program.cs
│ │ │ ├── Properties
│ │ │ │ └── launchSettings.json
│ │ │ └── Startup.cs
│ │ ├── FrontendWeb
│ │ │ ├── appsettings.json
│ │ │ ├── Controllers
│ │ │ │ └── HomeController.cs
│ │ │ ├── FrontendWeb.csproj
│ │ │ ├── Program.cs
│ │ │ ├── Properties
│ │ │ │ └── launchSettings.json
│ │ │ ├── Startup.cs
│ │ │ └── Views
│ │ │ ├── Home
│ │ │ │ ├── Index.cshtml
│ │ │ │ └── PlaceOrder.cshtml
│ │ │ └── _ViewImports.cshtml
│ │ ├── OrdersApi
│ │ │ ├── appsettings.json
│ │ │ ├── Controllers
│ │ │ │ └── OrdersController.cs
│ │ │ ├── DataStore
│ │ │ │ ├── Order.cs
│ │ │ │ └── OrdersDbContext.cs
│ │ │ ├── OrdersApi.csproj
│ │ │ ├── Program.cs
│ │ │ ├── Properties
│ │ │ │ └── launchSettings.json
│ │ │ └── Startup.cs
│ │ ├── Shared
│ │ │ ├── Constants.cs
│ │ │ ├── Customer.cs
│ │ │ ├── JaegerServiceCollectionExtensions.cs
│ │ │ ├── PlaceOrderCommand.cs
│ │ │ └── Shared.csproj
│ │ └── TrafficGenerator
│ │ ├── appsettings.json
│ │ ├── Program.cs
│ │ ├── TrafficGenerator.csproj
│ │ └── Worker.cs
│ ├── net7.0
│ │ ├── CustomersApi
│ │ │ ├── appsettings.json
│ │ │ ├── CustomersApi.csproj
│ │ │ ├── DataStore
│ │ │ │ └── CustomerDbContext.cs
│ │ │ ├── Program.cs
│ │ │ └── Properties
│ │ │ └── launchSettings.json
│ │ ├── FrontendWeb
│ │ │ ├── appsettings.json
│ │ │ ├── Controllers
│ │ │ │ └── HomeController.cs
│ │ │ ├── FrontendWeb.csproj
│ │ │ ├── Program.cs
│ │ │ ├── Properties
│ │ │ │ └── launchSettings.json
│ │ │ └── Views
│ │ │ ├── Home
│ │ │ │ ├── Index.cshtml
│ │ │ │ └── PlaceOrder.cshtml
│ │ │ └── _ViewImports.cshtml
│ │ ├── OrdersApi
│ │ │ ├── appsettings.json
│ │ │ ├── Controllers
│ │ │ │ └── OrdersController.cs
│ │ │ ├── DataStore
│ │ │ │ ├── Order.cs
│ │ │ │ └── OrdersDbContext.cs
│ │ │ ├── OrdersApi.csproj
│ │ │ ├── Program.cs
│ │ │ └── Properties
│ │ │ └── launchSettings.json
│ │ ├── Shared
│ │ │ ├── Constants.cs
│ │ │ ├── Customer.cs
│ │ │ ├── JaegerServiceCollectionExtensions.cs
│ │ │ ├── PlaceOrderCommand.cs
│ │ │ └── Shared.csproj
│ │ └── TrafficGenerator
│ │ ├── appsettings.json
│ │ ├── Program.cs
│ │ ├── TrafficGenerator.csproj
│ │ └── Worker.cs
│ └── netcoreapp3.1
│ ├── CustomersApi
│ │ ├── appsettings.json
│ │ ├── Controllers
│ │ │ └── CustomersController.cs
│ │ ├── CustomersApi.csproj
│ │ ├── DataStore
│ │ │ └── CustomerDbContext.cs
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ └── Startup.cs
│ ├── FrontendWeb
│ │ ├── appsettings.json
│ │ ├── Controllers
│ │ │ └── HomeController.cs
│ │ ├── FrontendWeb.csproj
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── Startup.cs
│ │ └── Views
│ │ ├── Home
│ │ │ ├── Index.cshtml
│ │ │ └── PlaceOrder.cshtml
│ │ └── _ViewImports.cshtml
│ ├── OrdersApi
│ │ ├── appsettings.json
│ │ ├── Controllers
│ │ │ └── OrdersController.cs
│ │ ├── DataStore
│ │ │ ├── Order.cs
│ │ │ └── OrdersDbContext.cs
│ │ ├── OrdersApi.csproj
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ └── Startup.cs
│ ├── Shared
│ │ ├── Constants.cs
│ │ ├── Customer.cs
│ │ ├── JaegerServiceCollectionExtensions.cs
│ │ ├── PlaceOrderCommand.cs
│ │ └── Shared.csproj
│ └── TrafficGenerator
│ ├── appsettings.json
│ ├── Program.cs
│ ├── TrafficGenerator.csproj
│ └── Worker.cs
├── SignKey.snk
├── src
│ ├── Directory.Build.props
│ └── OpenTracing.Contrib.NetCore
│ ├── AspNetCore
│ │ ├── AspNetCoreDiagnosticOptions.cs
│ │ ├── AspNetCoreDiagnostics.cs
│ │ ├── HostingOptions.cs
│ │ └── RequestHeadersExtractAdapter.cs
│ ├── Configuration
│ │ ├── DiagnosticOptions.cs
│ │ ├── IOpenTracingBuilder.cs
│ │ ├── OpenTracingBuilder.cs
│ │ ├── OpenTracingBuilderExtensions.cs
│ │ └── ServiceCollectionExtensions.cs
│ ├── EntityFrameworkCore
│ │ ├── EntityFrameworkCoreDiagnosticOptions.cs
│ │ └── EntityFrameworkCoreDiagnostics.cs
│ ├── GenericListeners
│ │ ├── GenericDiagnosticOptions.cs
│ │ └── GenericDiagnostics.cs
│ ├── HttpHandler
│ │ ├── HttpHandlerDiagnosticOptions.cs
│ │ ├── HttpHandlerDiagnostics.cs
│ │ └── HttpHeadersInjectAdapter.cs
│ ├── InstrumentationService.cs
│ ├── Internal
│ │ ├── DiagnosticEventObserver.cs
│ │ ├── DiagnosticManager.cs
│ │ ├── DiagnosticManagerOptions.cs
│ │ ├── DiagnosticObserver.cs
│ │ ├── GenericEventProcessor.cs
│ │ ├── GlobalTracerAccessor.cs
│ │ ├── IGlobalTracerAccessor.cs
│ │ ├── PropertyFetcher.cs
│ │ ├── SpanExtensions.cs
│ │ └── TracerExtensions.cs
│ ├── Logging
│ │ ├── OpenTracingLogger.cs
│ │ └── OpenTracingLoggerProvider.cs
│ ├── MicrosoftSqlClient
│ │ ├── MicrosoftSqlClientDiagnosticOptions.cs
│ │ └── MicrosoftSqlClientDiagnostics.cs
│ ├── OpenTracing.Contrib.NetCore.csproj
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── SystemSqlClient
│ ├── SqlClientDiagnosticOptions.cs
│ └── SqlClientDiagnostics.cs
├── test
│ └── OpenTracing.Contrib.NetCore.Tests
│ ├── AspNetCore
│ │ └── HostingTest.cs
│ ├── CoreFx
│ │ └── HttpHandlerDiagnosticTest.cs
│ ├── Internal
│ │ ├── DiagnosticManagerTest.cs
│ │ └── PropertyFetcherTest.cs
│ ├── Logging
│ │ ├── LoggingDependencyInjectionTest.cs
│ │ └── LoggingTest.cs
│ ├── OpenTracing.Contrib.NetCore.Tests.csproj
│ └── XunitLogging
│ ├── XunitLoggerFactoryExtensions.cs
│ └── XunitLoggerProvider.cs
└── version.props
72 directories, 158 files
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论