如何关联elasticsearch中的日志

How to correlate logs in elasticsearch

我有一个 asp.net 核心网络应用程序 (mvc) 项目,当应用程序中发生某些事情时,它会将日志写入 elasticsearch 并从 elasticsearch 读取日志。

有没有一种方法可以关联同一浏览器事务中发生的日志? (即打开浏览器,用户单击每个页面上的按钮将他们重定向到另一个页面,直到他们到达页面的末尾,然后关闭浏览器)我看到了一些关于 APM 的信息,但是 有其他选择吗将这些日志组合为一个事务 如果这有意义的话。


更新:

我完全按照 apm installation tutorial guide 对 Windows 的说明进行了操作。当我到达第 3 步启动 APM 服务器时,它对 运行 cmd Start-Service apm-server 说但没有任何反应。因此,当我尝试使用 cmd ./apm-server -e 启动服务器时,它做了一些看起来不像它应该以管理员身份在 powershell 中显示的格式的东西。

然后我检查了 apm 服务器状态,它显示设置正确。我没有执行第 2 步,因为我 运行 将其设置为本地主机。

进入最后一步,即 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, 
               new HttpDiagnosticsSubscriber(), // 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 文件应用程序上看到的 运行ning:

我运行程序并在浏览器上执行一些操作。然后我去检查代理状态,它说还没有收到来自代理的数据。但我完全按照他们的要求做了。

我是不是漏掉了指南中没有的东西?

使用 APM 将是实现此目的的最简单方法,因为它非常简单 install and get running

.Net APM Agent 理解事务的概念并且可以在 log correlation(Serilog 或 NLog)

方面提供很大帮助