gpt4 book ai didi

c# - 如何配置 .NET Core Azure 日志记录

转载 作者:行者123 更新时间:2023-11-30 21:32:34 25 4
gpt4 key购买 nike

我在 .NET Core 2.1.3 上的 Azure 中上传了一个非常简单的应用程序。
配置应用服务日志记录如下: enter image description here

代码:

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...");
});
}
}

问题是日志记录可以在本地工作,但在 azure 中不行。如果我打开/Log Files/Application/afg4322-201809062011.log我的消息 OnRequest::::::::::::::::::Loading::::::::::::::::::没有出现在那里。当前逻辑捕获所有请求并简单地写入日志消息。
我还安装了Microsoft.Extensions.Logging.AzureAppServices和故障排除https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/azure-apps/troubleshoot?view=aspnetcore-2.1#aspnet-core-module-stdout-log没有任何作用。

如何在 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...");

我从 loggerFactory 获取一个记录器,而不是使用字段 logger

现在,在 Azure 门户中接收流日志中的所有消息:

2018-09-06 19:10:01.449 +00:00 [信息] WebApp.Startup: OnRequest:::::::::Info::::::::::
2018-09-06 19:10:01.449 +00:00 [调试] WebApp.Startup: OnRequest:::::::::调试::::::::::
2018-09-06 19:10:01.449 +00:00 [错误] WebApp.Startup: OnRequest:::::::::::::::错误::::
2018-09-06 19:10:01.449 +00:00 [警告] WebApp.Startup: OnRequest::::::::::::警告::::::
2018-09-06 19:10:01.449 +00:00 [Trace] WebApp.Startup: OnRequest::::::::::::::Trace:::::
2018-09-06 19:10:01.449 +00:00 [严重] WebApp.Startup: OnRequest::::::::::::::::严重::::

关于c# - 如何配置 .NET Core Azure 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52209561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com