【发布时间】:2023-01-09 18:43:19
【问题描述】:
我正在尝试记录来自我的 .NET 应用程序的自定义消息,但我在 Azure Application Insights 中看不到跟踪日志。 这是我的 Program.cs 中的内容
var config = TelemetryConfiguration.CreateDefault();
config.ConnectionString = builder.Configuration.GetSection("ApplicationInsights").GetSection("ConnectionString").Value;
Log.Logger = new LoggerConfiguration()
.CreateBootstrapLogger();
builder.Host.UseSerilog((context, services, configuration) => configuration
.WriteTo.Console()
.WriteTo.File("Logs/log.txt")
.WriteTo.ApplicationInsights(new TelemetryClient(config), TelemetryConverter.Traces, Serilog.Events.LogEventLevel.Warning));
builder.Services.AddApplicationInsightsTelemetry();
app.UseSerilogRequestLogging();
我正在从控制器发送消息
public async Task<IActionResult> Get()
{
logger.Warning("Custom log from controller");
return Ok();
}
这是我的应用程序设置文件
{
"Serilog": {
"Using": [
"Serilog.Sinks.ApplicationInsights"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"restrictedToMinimumLevel": "Warning"
}
},
{
"Name": "File",
"Args": {
"restrictedToMinimumLevel": "Warning",
"path": "Logs/log.txt",
"rollingInterval": "Day"
}
}
]
},
"ApplicationInsights": {
"ConnectionString": "myKey",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
},
还有我的 csproj 文件
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
</ItemGroup>
</Project>
如果我将 TelemetryConverter 更改为事件而不是跟踪,我可以看到消息,但为什么我在那里看不到跟踪日志? 如果我在 Program.cs 中进行配置,是否需要在 appsettings 中配置“Serilog”部分
【问题讨论】:
-
请分享您的
.csproj和appsettings.json文件。 -
@Harshitha 我刚刚更新了问题
标签: c# .net logging azure-application-insights serilog