c# - 如何配置 .NET Core Azure 日志记录
问题描述
我在 .NET Core 2.1.3 上的 Azure 中上传了一个非常简单的应用程序。
如下配置应用服务日志记录:
编码:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
public class Startup
{
private readonly ILogger<Startup> logger;
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
this.Configuration = configuration;
this.logger = logger;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Commented lines doesn't affect ...
//loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
// loggerFactory.AddDebug();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
loggerFactory.AddAzureWebAppDiagnostics();
this.logger.LogInformation("Loading::::::::::::::::::");
app.Run(async (context) =>
{
this.logger.LogInformation("OnRequest::::::::::::::::::");
await context.Response.WriteAsync("Loading...");
});
}
}
问题是日志记录在本地工作,但在天蓝色没有。如果我打开/Log Files/Application/afg4322-201809062011.log
我的消息OnRequest::::::::::::::::::
但Loading::::::::::::::::::
没有出现在那里。目前,该逻辑捕获所有请求并简单地写入一条日志消息。
我还安装Microsoft.Extensions.Logging.AzureAppServices
并解决了问题https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/azure-apps/troubleshoot?view=aspnetcore-2.1#aspnet-core-module-stdout -记录没有任何工作。
如何在 Azure 应用中记录消息?也许我错过了一些简单的设置?
applicationSettings.json
: _
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"IncludeScopes": "true"
}
}
}
我查看了这篇文章 - Asp.net Core azure web app logging - How to enable Application Logs in Azure for Net Core 2 App?
我试图避免来自Ilya Chernomordik
他所说的设置<aspNetCore stdoutLogEnabled="true" />
和修改的建议SourceSwitch
。我认为这不是正确的解决方案。
解决方案
A找到了解决办法。
该应用程序以Release
模式发布 - 所以我添加了一个appsettings.Production.json
:
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
}
}
并将我的代码更改如下:
var l = loggerFactory.CreateLogger<Startup>();
l.LogInformation("OnRequest:::::::::Info:::::::::");
l.LogDebug("OnRequest:::::::::Debug:::::::::");
l.LogError("OnRequest::::::::::::::Error::::");
l.LogWarning("OnRequest::::::::::::Warning::::::");
l.LogTrace("OnRequest:::::::::::::Trace:::::");
l.LogCritical("OnRequest::::::::::::::Critical::::");
await context.Response.WriteAsync("Loading...");
而是使用该字段logger
,我从loggerFactory
.
Streaming Logs
现在在 Azure 门户中接收所有消息:
2018-09-06 19:10:01.449 +00:00 [Information] WebApp.Startup: OnRequest:::::::::Info:::::::::
2018-09-06 19:10:01.449 +00:00 [Debug] WebApp.Startup: OnRequest:::::::::Debug:::::::::
2018-09-06 19:10:01.449 +00:00 [Error] WebApp.Startup: OnRequest::::::::::::::Error::::
2018-09-06 19:10:01.449 +00:00 [Warning] WebApp.Startup: OnRequest::::::::::::Warning::::::
2018-09-06 19:10:01.449 +00:00 [Trace] WebApp.Startup: OnRequest:::::::::::::Trace:::::
2018-09-06 19:10:01.449 +00:00 [Critical] WebApp.Startup: OnRequest::::::::::::::Critical::::
推荐阅读
- html - 是否可以在父元素的 onclick 处理程序之前有一个触发器?
- google-cloud-platform - 如何从共享 CPU 实例禁用 Google Cloud Plat 上的突发?
- python - 如何输出我的数据集的地图?
- python - 在 Pandas 中绘图不会显示所有数据
- selenium-webdriver - 尝试从 excel 中获取数据时出现数据提供程序不匹配错误
- vba - 根据关键字查找要复制的特定范围
- docker - 在 docker 上安装特定版本的弹性搜索
- c# - 如何使用 asp.net 从 Sharepoint 中检索 30 多个文档?
- ffmpeg - ffmpeg:我不能让 tee 为 restreaming 工作
- python - ValueError: matplotlib display text must have all code points < 128 or use Unicode strings (Using a path)