没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2025-05-28 10:13:16.470|阅读 25 次
概述:本文将为大家介绍DevExpress XAF将.NET Aspire集成到Blazor项目中后如何实现自定义遥测,欢迎下载最新版组件体验!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
DevExpress XAF是一款强大的现代应用程序框架,允许同时开发ASP.NET和WinForms。DevExpress XAF采用模块化设计,开发人员可以选择内建模块,也可以自行创建,从而以更快的速度和比开发人员当前更强有力的方式创建应用程序。
.NET Aspire是一组工具、模板和包,用于构建可观察的、可生产的应用程序。DevExpress XAF团队花费了一些时间考虑Aspire的功能,试图找到最好的集成点,让XAF开发人员能够利用Aspire开箱即用的业务流程特性。
DevExpress技术交流群11:749942875 欢迎一起进群讨论
在最近的一篇文章中我们介绍了如何对一个 XAF Blazor 项目进行调整,来支持 .NET Aspire()。通过对启动逻辑进行一些修改——包括标准的 XAF 项目模板和 Aspire 的 Visual Studio 向导生成的代码,已经可以让 XAF Blazor 项目作为 Aspire 编排体系的一部分运行了。但那只是最小规模的编排,只有一个模块!接下来我们将会把部署方面的内容留到第三篇(敬请关注!),接下来得系列文章将介绍在示例项目中为实现以下三个场景所做的修改:
完整示例项目已托管在这个中。下面基于我在第一篇文章中描述的项目初始状态,展开说明新的功能实现。
Aspire 仪表盘的集成,是许多应用项目在启用 Aspire 后最直观的新特性之一。正如在演示项目的第一步中看到的,启用默认的跟踪和指标来源非常简单。接下来我们就会思考:如何将遥测系统用于我们自己的场景,比如记录自定义日志、报告业务指标与活动?
第一步,我创建了文件 XafAspireDemo.Blazor.Server/Controllers/ImportantBusinessOperationsController.cs,它实现了一个基本的 XAF 控制器,并提供了一个动作(Action)。该操作会自动出现在用户界面中,因此可以用作交互式测试触发器。以下是代码:
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(); } } }
如你所见,这段代码已经加入了日志记录的逻辑。它使用的是 .NET 提供的标准 ILogger<T> 接口,并结合了构造函数依赖注入(通过 IServiceProvider 获取服务实例),这是XAF推荐的方式。
ASP.NET Core 的日志基础设施会自动将这些日志输出集成到 Aspire 仪表盘中。完成上述更改后,运行程序并点击Important Business Action按钮,即可在仪表盘中查看相关日志输出。按钮会被随机禁用一段时间,并在初始化与交互过程开始时输出日志内容,在仪表盘的“结构化日志(Structured Logs)”页面即可查看这些信息。
要记录 OpenTelemetry 的活动(Activity)并使用计量器(Meter),您需要在应用启动时初始化这些对象,并确保相关代码能够访问到它们。可以通过全局单例模式实现,但在这个示例项目中,更合理的方式是继续使用依赖注入机制,并让它来处理生命周期管理。
以下是 XafAspireDemo.Blazor.Server.Telemetry 类的实现:
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> 类型都来自 System.Diagnostics.Metrics 命名空间。这个类在应用启动时创建相关对象,在应用结束时自动释放。
您只需要在 Startup.cs 中注册该类为单例服务即可:
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<>),
接下来我们要将遥测功能应用到前面创建的业务操作中,只需在 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 --> ); } }
在方法开始处,我们通过依赖注入获取遥测服务,然后开启一个活动(Activity)。如果执行过程中发生错误,会记录异常信息,并将活动状态设为错误,最后记录计数器和直方图的相关指标值。
这样,当您执行该业务操作时,Aspire 仪表盘便能显示相应的指标与活动信息。值得注意的是,OpenTelemetry 会自动关联活动与指标(如执行时长记录),不需要你手动建立连接。
未完待续,我们下期再见!更多产品资讯及授权,欢迎来电咨询:023-68661681
慧都是⼀家⾏业数字化解决⽅案公司,专注于软件、⽯油与⼯业领域,以深⼊的业务理解和⾏业经验,帮助企业实现智能化转型与持续竞争优势。
慧都科技是DevExpress的中国区的合作伙伴,DevExpress作为用户界面领域的优秀产品,帮助企业高效构建权限管理、数据可视化(如网格/图表/仪表盘)、跨平台系统(WinForms/ASP.NET/.NET MAUI)及行业定制解决方案,加速开发并强化交互体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@dpuzeg.cn
文章转载自:慧都网本文将为大家介绍DevExpress XAF将.NET Aspire集成到Blazor项目中后如何实现数据库依赖,欢迎下载最新版组件体验!
FP3 文件是使用流行的报表生成工具FastReport创建的报表。这种格式广泛用于存储可立即查看的报表数据,这些数据可以轻松共享或保存以供日后分析。但是,要打开和查看此类文件,需要一个特殊的程序——FastReport Viewer。
邮件合并功能让您能够轻松批量创建个性化文档,例如信函、电子邮件、发票或证书。在本文中,我们将向您展示如何使用 C# 从 Excel 执行邮件合并。
将 HTML 内容转换为 Word 文档,对于内容共享、归档以及保持格式一致性都非常重要。本文将介绍如何使用 Spire.Doc for Java 在 Java 中将 HTML 文件或 HTML 字符串转换为 Word 文档。
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@dpuzeg.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢