我们终于在我们的服务架构集群中正确配置了EventSource和ElasticSearch。现在我们已经想要将EventSources添加到与我们的服务结构应用程序交互的Web应用程序中,以便我们可以在一个位置查看所有事件(应用程序日志)并通过Kibana过滤/查询。
我们的问题似乎与作为exe的服务结构应用程序和无状态的.NET 4.6(非.net CORE)Web应用程序之间的差异有关。在Service Fabric中,我们放置了在Program.cs中实例化管道的using语句,并设置了无限睡眠。
private static void Main()
{
try
{
using (var diagnosticsPipeline = ServiceFabricDiagnosticPipelineFactory.CreatePipeline("CacheApp-CacheAPI-DiagnosticsPipeline"))
{
ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Endpoint).Name);
// Prevents this host process from terminating so services keeps running.
Thread.Sleep(Timeout.Infinite);
}
如何在网络应用中执行此操作?这是我们用于EventSource的非ServiceFabric实现的管道代码。这就是我们正在使用的:
using (var pipeline = DiagnosticPipelineFactory.CreatePipeline("eventFlowConfig.json"))
{
IEnumerable ie = System.Diagnostics.Tracing.EventSource.GetSources();
ServiceEventSource.Current.Message("initialize eventsource");
}
我们能够看到管道并从using语句中向ElasticSearch发送事件,但不在其外部。所以问题是:
这就是这个问题,请告诉我你是否需要澄清。我看到很多doco用于客户端应用程序,但对于网络应用程序来说并不多。
谢谢, 格雷格
更新解决方案代码
DiagnosticPipeline管道;
protected void Application_Start(Object sender, EventArgs e)
{
try
{
pipeline = DiagnosticPipelineFactory.CreatePipeline("eventFlowConfig.json");
IEnumerable ie = System.Diagnostics.Tracing.EventSource.GetSources();
AppEventSource.Current.Message("initialize eventsource");
}
}
protected void Application_End(Object sender, EventArgs e)
{
pipeline.Dispose();
}
答案 0 :(得分:1)
假设ASP.NET Core初始化EventFlow管道的最简单方法是在Program.cs Main()
方法中,例如:
public static void Main(string[] args)
{
using (var pipeline = DiagnosticPipelineFactory.CreatePipeline("eventFlowConfig.json"))
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
这利用了host.Run()将在服务器关闭之前阻塞的事实,因此在接收和提供请求期间,管道将存在。
根据您使用的Web框架,可能会有所不同。例如。如果你使用的那个提供&#34;设置&#34;和#34;清理&#34;钩子,你可以在设置阶段创建一个诊断管道(并在一些成员变量中存储对它的引用),然后在清理阶段处理它。例如,在ASP.NET classic中,您将代码放在global.asax.cs
中并利用Application_OnStart
和Application_OnEnd
方法。有关详细信息,请参阅Application Instances, Application Events, and Application State in ASP.NET。
每次提供请求时创建一个管道实例是非常低效的,就像你说的那样。没有充分的理由这样做。