我一直在尝试在ASP.NET Core 2.0应用程序中设置Application Insights。当我在本地运行应用程序时,日志按预期显示在Application Insights中。但是,当部署到Azure应用服务时,虽然日志被发送到Application Insights的“请求”表中,但没有日志显示在“异常”或“跟踪”中。
唯一似乎解决这个问题的方法是将下面这行代码添加到
上述解决方案不理想,因为我们希望通过环境来配置日志级别,所以最好采用基于配置的解决方案。此外,我猜测问题与配置相关,因为在本地正常工作,Azure中没有进行任何特殊配置。
以下是完整的
我的Program.cs代码如下:
唯一似乎解决这个问题的方法是将下面这行代码添加到
Startup.Configure()
中:loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);
上述解决方案不理想,因为我们希望通过环境来配置日志级别,所以最好采用基于配置的解决方案。此外,我猜测问题与配置相关,因为在本地正常工作,Azure中没有进行任何特殊配置。
以下是完整的
Startup.Configure()
:public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);
}
我的Program.cs代码如下:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) => WebHost
.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
}
appsettings.json(InstrumentationKey
已更改以保护隐私):
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Debug"
}
},
"Console": {
"LogLevel": {
"Default": "Debug"
}
},
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ApplicationInsights": {
"InstrumentationKey": "00000000-0000-0000-0000-000000000000"
}
}