英文:
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。
-
我使用了您分享的相同配置,在
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 中的跟踪信息:
英文:
Initially even I got the traces only in Local, unable to send traces to Application Insights.
-
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 theLoggerConfiguration
. -
Install the below NuGet Packages
Serilog
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
My .csproj
file:
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
- 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("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");
---
}
Traces in Application Insights:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论