我们正在尝试为 ASP Net Core 7.0 C# 应用程序启用 Application Insights。我们使用了一些来源,包括这个 Microsoft 网站:
Azure Application Insights 模块本身似乎配置正确,例如它正确显示应用程序请求。
但是,C# 应用程序中的自定义 ILogger 消息不会显示在 Azure App Insights
traces
部分中。
我们的设置基于如何配置 ILogging 的各种其他示例,因此我们不确定设置的哪一方面是错误的,但怀疑它在我们的代码中的某个地方。
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddApplicationInsightsTelemetry();
builder.Logging.AddApplicationInsights(
configureTelemetryConfiguration: config =>
config.ConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
configureApplicationInsightsLoggerOptions: options => { }
);
...
var app = builder.Build();
}
Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"ApplicationInsights": "Debug"
}
}
<ItemGroup>
...
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
...
</ItemGroup>
下面的配置中有什么看起来不正确的地方吗?
是的,配置错误,通过检查以下配置来看看正确的方法:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Debug"
}
}
}
}
如您所见,ApplicationInsights 日志级别是在一个小节中设置的。
感谢@Peter Bons 的评论。
是的,正如 Peter Bons 提到的,您的配置文件没有所有必需的设置。
如果您在本地运行
ASP Net Core C# application
,那么我们需要在 Connection String/Instrumentation Key
文件中设置 Application Insights appsettings.json
。
我的
.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" />
</ItemGroup>
</Project>
我的
appsettings.json
文件:
{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Error"
}
},
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=**********;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/"
}
}
我的
Program.cs
文件:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddApplicationInsightsTelemetry();
//var abc = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
我的
Controller.cs
:
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogWarning("Test Warning trace - June 5");
_logger.LogError("Test Error - June 5");
return View();
}
Application Insights 交易搜索:
您可以看到我也能够记录异常。
将 Azure Function 迁移到 .NET 8 上的隔离进程模型后,我遇到了同样的问题。
该问题是由 Application Insights SDK 的默认行为引起的,该行为添加了一个日志记录筛选器,指示记录器仅捕获警告和更严重的日志。您可以在这里找到更多详细信息
为了解决此问题,我按照链接中的建议将
ConfigureLogging()
添加到 Program.cs
文件中: