我正在使用EventFlow跟蹤ETW事件。為此,我創(chuàng)建了一個充當偵聽器的ASP Net Core服務(wù)。我已經(jīng)在配置文件中配置了自己的自定義輸出。這些是我的Output和OutputFactory類:class CustomOutput : IOutput{ public Task SendEventsAsync(IReadOnlyCollection<EventData> events, long transmissionSequenceNumber, CancellationToken cancellationToken) { foreach(var e in events) { //...; } return Task.CompletedTask; }}class CustomOutputFactory : IPipelineItemFactory<CustomOutput>{ public CustomOutput CreateItem(IConfiguration configuration, IHealthReporter healthReporter) { return new CustomOutput(); }}此CustomOutput在啟動時(創(chuàng)建EventFlow管道時)僅實例化一次,并且用于所有事件。主要方法是這樣的:private static void Main(){ try { using (var diagnosticsPipeline = ServiceFabricDiagnosticPipelineFactory.CreatePipeline("MyApplication-MyService-DiagnosticsPipeline")) { ServiceRuntime.RegisterServiceAsync("Stateless1Type", context => new Stateless1(context)).GetAwaiter().GetResult(); ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Stateless1).Name); Thread.Sleep(Timeout.Infinite); } } catch (Exception e) { ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString()); throw; }}輸出和工廠輸出類型在配置文件eventFlowConfig.json中引用:"extensions": [{ "category": "outputFactory", "type": "CustomOutput", "qualifiedTypeName": "MyProyect.Service.MyApp.SqlOutputFactory, MyProyect.Service.MyApp"}]因此,實例是在Program類的main方法中創(chuàng)建的,即在調(diào)用Startup配置方法之前。如果容器在實例化時仍然不存在,如何從我的Output類訪問我的依賴項容器服務(wù)?目前,我已經(jīng)創(chuàng)建了IServiceCollection類型的靜態(tài)屬性,并通過“啟動配置”方法(使用setter注入)對其進行了設(shè)置。我不喜歡這種解決方案,因為我不應(yīng)該使用對服務(wù)的靜態(tài)訪問,但是我不知道其他解決方案。這是有效的做法嗎?
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報
0/150
提交
取消