【发布时间】:2019-01-10 10:45:38
【问题描述】:
我一直在尝试使用 ASP.NET Core 2.0 应用程序设置 Application Insights。在本地运行我的应用程序时,日志会按预期显示在 Application Insights 中。但是,当部署到 Azure 应用服务时,当日志被发送到 Application Insights 中的“请求”表时,“异常”或“跟踪”中不会显示任何日志。
似乎解决这个问题的唯一方法是将以下代码行添加到 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如下:
namespace TestLoggingApplication
{
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"
}
}
【问题讨论】:
标签: c# asp.net-core .net-core azure-application-insights azure-app-service-envrmnt