Serilog Application Insights Sink显示请求但不显示跟踪。

huangapple go评论60阅读模式
英文:

Serilog Application Insights Sink is showing Requests but not Traces

问题

我在一个.NET项目中使用了Serilog应用程序洞察器接口,它在Azure门户中显示请求,但没有显示跟踪信息。

在VS中的应用程序洞察器遥测窗口显示了本地应用程序洞察器,其中显示了请求和跟踪信息,但在Azure门户中只能看到请求。

我尝试了使用最小配置使其工作,但我根本看不到跟踪信息。我有一个文件接口,显示了日志消息和请求。

我错过了什么?

var logger = new LoggerConfiguration()
	.ReadFrom.Configuration(configuration)
    .CreateLogger();

builder.Host.UseSerilog(logger);

builder.Services.AddApplicationInsightsTelemetry();

_logger.LogInformation("Test Log Entry");

我使用的配置文件:

{
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=XXXX",
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "O:\\logs\\log1.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
          "rollingInterval": "Day",
          "fileSizeLimitBytes": 20000000,
          "rollOnFileSizeLimit": true
        }
      },
      {
        "Name": "ApplicationInsights",
        "Args": {
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}
英文:

I am using the serilog application insights sink in a .NET project and it is showing requests but not traces in the Azure portal.

The application insights telemetry window in VS shows local app insights which is showing the requests and traces but in the azure portal I can only see requests.
I have tried to use the minimal configuration to get it working but I just cant see traces at all. I have a file sink which shows the log messages and the requests.
What am I missing?

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

builder.Host.UseSerilog(logger);

builder.Services.AddApplicationInsightsTelemetry();

_logger.LogInformation("Test Log Entry");

The config file I am using:

{
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=XXXX",
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "O:\\logs\\log1.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
          "rollingInterval": "Day",
          "fileSizeLimitBytes": 20000000,
          "rollOnFileSizeLimit": true
        }
      },
      {
        "Name": "ApplicationInsights",
        "Args": {
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}

答案1

得分: 0

以下是您要翻译的内容:

最初,我只能在本地获得跟踪信息,无法将跟踪信息发送到Application Insights。

Serilog Application Insights Sink显示请求但不显示跟踪。

  • 我使用了您分享的相同配置,在 Program.cs 文件中进行了更改。

  • 要使用 Serilog 记录请求/跟踪,我们需要使用 LoggerConfiguration

  • 安装以下 NuGet 包

Serilog
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights

我的 .csproj 文件:

<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
  • 我能够使用以下代码记录跟踪信息。

我的 Program.cs 文件:

using Serilog;
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;

var builder = WebApplication.CreateBuilder(args);

var Conn = builder.Configuration.GetSection("ApplicationInsights").GetValue<string>("ConnectionString");

var log = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.ApplicationInsights(Conn, new TraceTelemetryConverter())
    .CreateLogger();
builder.Logging.AddSerilog(log);
builder.Services.AddRazorPages();

var app = builder.Build();

app.Logger.LogInformation("Log Information from Program class file");
app.Logger.LogWarning("Log warning from Program.cs");
---
}

Application Insights 中的跟踪信息:

Serilog Application Insights Sink显示请求但不显示跟踪。

英文:

Initially even I got the traces only in Local, unable to send traces to Application Insights.

Serilog Application Insights Sink显示请求但不显示跟踪。

  • I have used the same configuration which you have shared, done changes in the Program.cs file.

  • To log requests/Traces using Serilog, we need to use the LoggerConfiguration.

  • Install the below NuGet Packages

Serilog
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights

My .csproj file:

 &lt;PackageReference Include=&quot;Serilog&quot; Version=&quot;3.0.1&quot; /&gt;
 &lt;PackageReference Include=&quot;Serilog.AspNetCore&quot; Version=&quot;6.1.0&quot; /&gt;
 &lt;PackageReference Include=&quot;Serilog.Sinks.ApplicationInsights&quot; Version=&quot;4.0.0&quot; /&gt;    
  • Iam able to log the traces using the below code.

My Program.cs file:

using Serilog;
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;

var builder = WebApplication.CreateBuilder(args);

var Conn = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;string&gt;(&quot;ConnectionString&quot;);

var log = new LoggerConfiguration()
    .Enrich.FromLogContext()
 .WriteTo.ApplicationInsights(Conn, new TraceTelemetryConverter())
 .CreateLogger();
builder.Logging.AddSerilog(log);
builder.Services.AddRazorPages();

var app = builder.Build();

app.Logger.LogInformation(&quot;Log Information from Program class file&quot;);
app.Logger.LogWarning(&quot;Log warning from Program.cs&quot;);
---
}

Traces in Application Insights:

Serilog Application Insights Sink显示请求但不显示跟踪。

huangapple
  • 本文由 发表于 2023年7月31日 23:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定