翻譯|使用教程|編輯:龔雪|2025-05-28 10:13:16.470|閱讀 109 次
概述:本文將為大家介紹DevExpress XAF將.NET Aspire集成到Blazor項(xiàng)目中后如何實(shí)現(xiàn)自定義遙測(cè),歡迎下載最新版組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress XAF是一款強(qiáng)大的現(xiàn)代應(yīng)用程序框架,允許同時(shí)開發(fā)ASP.NET和WinForms。DevExpress XAF采用模塊化設(shè)計(jì),開發(fā)人員可以選擇內(nèi)建模塊,也可以自行創(chuàng)建,從而以更快的速度和比開發(fā)人員當(dāng)前更強(qiáng)有力的方式創(chuàng)建應(yīng)用程序。
.NET Aspire是一組工具、模板和包,用于構(gòu)建可觀察的、可生產(chǎn)的應(yīng)用程序。DevExpress XAF團(tuán)隊(duì)花費(fèi)了一些時(shí)間考慮Aspire的功能,試圖找到最好的集成點(diǎn),讓XAF開發(fā)人員能夠利用Aspire開箱即用的業(yè)務(wù)流程特性。
DevExpress技術(shù)交流群11:749942875 歡迎一起進(jìn)群討論
在最近的一篇文章中我們介紹了如何對(duì)一個(gè) XAF Blazor 項(xiàng)目進(jìn)行調(diào)整,來(lái)支持 .NET Aspire()。通過(guò)對(duì)啟動(dòng)邏輯進(jìn)行一些修改——包括標(biāo)準(zhǔn)的 XAF 項(xiàng)目模板和 Aspire 的 Visual Studio 向?qū)傻拇a,已經(jīng)可以讓 XAF Blazor 項(xiàng)目作為 Aspire 編排體系的一部分運(yùn)行了。但那只是最小規(guī)模的編排,只有一個(gè)模塊!接下來(lái)我們將會(huì)把部署方面的內(nèi)容留到第三篇(敬請(qǐng)關(guān)注!),接下來(lái)得系列文章將介紹在示例項(xiàng)目中為實(shí)現(xiàn)以下三個(gè)場(chǎng)景所做的修改:
完整示例項(xiàng)目已托管在這個(gè)中。下面基于我在第一篇文章中描述的項(xiàng)目初始狀態(tài),展開說(shuō)明新的功能實(shí)現(xiàn)。
Aspire 儀表盤的集成,是許多應(yīng)用項(xiàng)目在啟用 Aspire 后最直觀的新特性之一。正如在演示項(xiàng)目的第一步中看到的,啟用默認(rèn)的跟蹤和指標(biāo)來(lái)源非常簡(jiǎn)單。接下來(lái)我們就會(huì)思考:如何將遙測(cè)系統(tǒng)用于我們自己的場(chǎng)景,比如記錄自定義日志、報(bào)告業(yè)務(wù)指標(biāo)與活動(dòng)?
第一步,我創(chuàng)建了文件 XafAspireDemo.Blazor.Server/Controllers/ImportantBusinessOperationsController.cs,它實(shí)現(xiàn)了一個(gè)基本的 XAF 控制器,并提供了一個(gè)動(dòng)作(Action)。該操作會(huì)自動(dòng)出現(xiàn)在用戶界面中,因此可以用作交互式測(cè)試觸發(fā)器。以下是代碼:
namespace XafAspireDemo.Blazor.Server.Controllers { public class ImportantBusinessOperationsController : Controller { SimpleAction importantBusinessAction; IServiceProvider serviceProvider; public ImportantBusinessOperationsController() { importantBusinessAction = new SimpleAction( this, "ImportantBusinessAction", PredefinedCategory.View ); importantBusinessAction.Execute += ImportantBusinessAction_Execute; } [ActivatorUtilitiesConstructor] public ImportantBusinessOperationsController(IServiceProvider serviceProvider) : this() { this.serviceProvider = serviceProvider; } private async void ImportantBusinessAction_Execute( object sender, SimpleActionExecuteEventArgs e ) { var logger = serviceProvider.GetRequiredService< ILogger<ImportantBusinessOperationsController> >(); importantBusinessAction.Enabled["ImportantBusinessActionRunning"] = false; logger.LogInformation("ImportantBusinessAction started."); try { // This is where we perform the magic for the important business action. // Run a task that waits a random time between half a second and five seconds. await Task.Run(() => { Thread.Sleep(new Random().Next(500, 5000)); }); } catch (Exception ex) { logger.LogError(ex, "ImportantBusinessAction failed."); throw; } finally { importantBusinessAction.Enabled["ImportantBusinessActionRunning"] = true; } } protected override void OnActivated() { base.OnActivated(); var logger = serviceProvider.GetRequiredService< ILogger<ImportantBusinessOperationsController> >(); logger.LogInformation("ImportantBusinessOperationsController activated."); } protected override void OnDeactivated() { var logger = serviceProvider.GetRequiredService< ILogger<ImportantBusinessOperationsController> >(); logger.LogInformation("ImportantBusinessOperationsController deactivated."); base.OnDeactivated(); } } }
如你所見,這段代碼已經(jīng)加入了日志記錄的邏輯。它使用的是 .NET 提供的標(biāo)準(zhǔn) ILogger<T> 接口,并結(jié)合了構(gòu)造函數(shù)依賴注入(通過(guò) IServiceProvider 獲取服務(wù)實(shí)例),這是XAF推薦的方式。
ASP.NET Core 的日志基礎(chǔ)設(shè)施會(huì)自動(dòng)將這些日志輸出集成到 Aspire 儀表盤中。完成上述更改后,運(yùn)行程序并點(diǎn)擊Important Business Action按鈕,即可在儀表盤中查看相關(guān)日志輸出。按鈕會(huì)被隨機(jī)禁用一段時(shí)間,并在初始化與交互過(guò)程開始時(shí)輸出日志內(nèi)容,在儀表盤的“結(jié)構(gòu)化日志(Structured Logs)”頁(yè)面即可查看這些信息。
要記錄 OpenTelemetry 的活動(dòng)(Activity)并使用計(jì)量器(Meter),您需要在應(yīng)用啟動(dòng)時(shí)初始化這些對(duì)象,并確保相關(guān)代碼能夠訪問(wèn)到它們。可以通過(guò)全局單例模式實(shí)現(xiàn),但在這個(gè)示例項(xiàng)目中,更合理的方式是繼續(xù)使用依賴注入機(jī)制,并讓它來(lái)處理生命周期管理。
以下是 XafAspireDemo.Blazor.Server.Telemetry 類的實(shí)現(xiàn):
namespace XafAspireDemo.Blazor.Server { public class Telemetry : IDisposable { public ActivitySource ActivitySource { get; } public Meter Meter { get; } public string MeterName => Meter.Name; public Counter<long> ImportantBusinessOperationCounter { get; } public Histogram<double> ImportantBusinessOperationDuration { get; } public Telemetry( string serviceName = "XafAspireDemo.Blazor.Server", string version = "1.0.0" ) { ActivitySource = new ActivitySource(serviceName, version); Meter = new Meter(serviceName, version); ImportantBusinessOperationCounter = Meter.CreateCounter<long>( "important_business_operation.execution_count" ); ImportantBusinessOperationDuration = Meter.CreateHistogram<double>( "important_business_operation.execution_duration" ); } public void Dispose() { ActivitySource.Dispose(); Meter.Dispose(); } } }
這里的 ActivitySource、Meter、Counter<T> 和 Histogram<T> 類型都來(lái)自 System.Diagnostics.Metrics 命名空間。這個(gè)類在應(yīng)用啟動(dòng)時(shí)創(chuàng)建相關(guān)對(duì)象,在應(yīng)用結(jié)束時(shí)自動(dòng)釋放。
您只需要在 Startup.cs 中注冊(cè)該類為單例服務(wù)即可:
builder.AddEntityFrameworkCoreInstrumentation(); }); --> var telemetry = new Telemetry(); --> services.AddSingleton(telemetry); --> services --> .AddOpenTelemetry() --> .WithTracing(tracing => tracing.AddSource("XafAspireDemo.Blazor.Server")) --> .WithMetrics(metrics => --> { --> metrics.AddMeter(telemetry.MeterName); --> }); services.AddSingleton( typeof(Microsoft.AspNetCore.SignalR.HubConnectionHandler<>),
接下來(lái)我們要將遙測(cè)功能應(yīng)用到前面創(chuàng)建的業(yè)務(wù)操作中,只需在 ImportantBusinessAction_Execute 方法中添加幾行代碼即可:
private async void ImportantBusinessAction_Execute( object sender, SimpleActionExecuteEventArgs e ) { --> var telemetry = serviceProvider.GetRequiredService<Telemetry>(); var logger = serviceProvider.GetRequiredService< ILogger<ImportantBusinessOperationsController> >(); importantBusinessAction.Enabled["ImportantBusinessActionRunning"] = false; --> using var activity = telemetry.ActivitySource.StartActivity("ImportantBusinessAction"); logger.LogInformation("ImportantBusinessAction started."); try { ... } catch (Exception ex) { logger.LogError(ex, "ImportantBusinessAction failed."); --> activity?.SetStatus(ActivityStatusCode.Error); --> activity?.AddException(ex); throw; } finally { --> activity?.Stop(); importantBusinessAction.Enabled["ImportantBusinessActionRunning"] = true; --> telemetry.ImportantBusinessOperationCounter.Add(1); --> telemetry.ImportantBusinessOperationDuration.Record( --> activity.Duration.TotalMilliseconds --> ); } }
在方法開始處,我們通過(guò)依賴注入獲取遙測(cè)服務(wù),然后開啟一個(gè)活動(dòng)(Activity)。如果執(zhí)行過(guò)程中發(fā)生錯(cuò)誤,會(huì)記錄異常信息,并將活動(dòng)狀態(tài)設(shè)為錯(cuò)誤,最后記錄計(jì)數(shù)器和直方圖的相關(guān)指標(biāo)值。
這樣,當(dāng)您執(zhí)行該業(yè)務(wù)操作時(shí),Aspire 儀表盤便能顯示相應(yīng)的指標(biāo)與活動(dòng)信息。值得注意的是,OpenTelemetry 會(huì)自動(dòng)關(guān)聯(lián)活動(dòng)與指標(biāo)(如執(zhí)行時(shí)長(zhǎng)記錄),不需要你手動(dòng)建立連接。
未完待續(xù),我們下期再見!更多產(chǎn)品資訊及授權(quán),歡迎來(lái)電咨詢:023-68661681
慧都是?家?業(yè)數(shù)字化解決?案公司,專注于軟件、?油與?業(yè)領(lǐng)域,以深?的業(yè)務(wù)理解和?業(yè)經(jīng)驗(yàn),幫助企業(yè)實(shí)現(xiàn)智能化轉(zhuǎn)型與持續(xù)競(jìng)爭(zhēng)優(yōu)勢(shì)。
慧都科技是DevExpress的中國(guó)區(qū)的合作伙伴,DevExpress作為用戶界面領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)高效構(gòu)建權(quán)限管理、數(shù)據(jù)可視化(如網(wǎng)格/圖表/儀表盤)、跨平臺(tái)系統(tǒng)(WinForms/ASP.NET/.NET MAUI)及行業(yè)定制解決方案,加速開發(fā)并強(qiáng)化交互體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)