问题描述
我有一个 asp.net 核心 Web 应用程序 (mvc) 项目,当应用程序中发生某些事情时,该项目将日志写入 elasticsearch 并从 elasticsearch 读取日志。
有没有一种方法可以关联同一浏览器事务中发生的日志? (即打开浏览器,用户点击每个页面上的按钮,将他们重定向到另一个页面,直到他们到达完成的页面末尾,然后关闭浏览器)我看到了一些关于 APM 的内容,但是 有没有组的替代方案如果有任何意义,这些日志会作为单个事务一起记录。
更新:
我完全按照 apm installation tutorial guide for Windows 中的说明进行操作。当我到第 3 步启动 APM 服务器时,它说运行 cmd Start-Service apm-server
但什么也没发生。因此,当我尝试使用 cmd ./apm-server -e
启动服务器时,它做了一些看起来不像它应该以管理员身份在 powershell 中显示的格式的内容。
然后我检查了 apm 服务器状态,它显示为正确设置。我没有执行第 2 步,因为我在 localhost 上运行它。
进入最后一步,即 APM 代理。我进入 .NET 选项卡并按照说明进行操作...安装 Elastic.APM.NetCoreAll nuget 包并执行以下操作...
Startup.cs
文件我添加了 app.UseElasticApm(Configuration,new HttpDiagnosticsSubscriber(),new EfCoreDiagnosticsSubscriber());
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
app.UseElasticApm(Configuration,// Enable tracing of outgoing HTTP requests
new EfCoreDiagnosticsSubscriber()); // Enable tracing of database calls through EF Core
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
在我的 appsettings.json
文件中:
{
"ApplicationName": "customer-simulation-es-app","Serilog": {
"MinimumLevel": {
"Default": "information","Override": {
"Microsoft": "information","System": "Warning"
}
}
},"ElasticConfiguration": {
"Uri": "http://localhost:9200"
},"ElasticApm": {
"ServerUrl": "http://localhost:8200","ServiceName": "CustomerApp","Environment": "production","CloudProvider": "none","LogLevel": "Trace"
},"AllowedHosts": "*"
}
这是我在程序开始运行时在 .exe 文件应用程序上看到的:
我运行程序并在浏览器上执行一些操作。然后我去检查代理状态,它说尚未从代理收到任何数据。但我完全按照他们的意愿去做。
我是否遗漏了指南中没有的内容?
解决方法
使用 APM 将是实现这一目标的最简单方法,因为它就像 install and get running 的馅饼一样简单。
.Net APM 代理了解事务的概念,并且可以为 log correlation(使用 Serilog 或 NLog)提供大量帮助