以前,当我在 .NET 中创建 Azure Webjobs 时,它会自动配置日志记录到 Azure 存储上路径/azure-webjobs-hosts/output-logs/.txt 上的文本文件。但是现在,当我使用 .NET Core 和 Webjobs SDK 3.0.14 版时,默认情况下它不再具有此行为。
日志记录到控制台和 Application Insights 非常适合我当前的设置,但我确实希望通过绑定(bind)的 ILogger 在 Azure 存储上进行文本文件日志记录。我已遵循有关如何配置它的官方指南,但它都指向存储队列,我不确定这是一个好方法以及如何调整它。
配置代码:
static async Task Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.UseEnvironment("development");
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
string instrumentationKey = context.Configuration["ApplicationInsightsKey"];
if (!string.IsNullOrEmpty(instrumentationKey))
{
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
}
});
builder.ConfigureServices(s => s.AddSingleton<IConfigurationRoot>(config));
var host = builder.Build();
using (host)
{
var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;
await host.StartAsync();
await jobHost.CallAsync("Run");
await host.StopAsync();
}
}
职位代码:
[NoAutomaticTrigger]
public async Task Run(ILogger logger)
{
logger.LogInformation("I want this text in console as well as on Azure Storage");
// ..logic
}
最佳答案
如果您想将日志(通过 ILogger)保存到 blob 存储,您需要将 webjob 发布到 azure。您可以按照以下步骤操作:
1.在builder.ConfigureLogging中添加
代码块:b.AddAzureWebAppDiagnostics();
方法(需要安装nuget包Microsoft.Extensions.Logging.AzureAppServices
)
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
b.AddAzureWebAppDiagnostics();
//other code
});
2.发布到azure,然后在azure门户->相关应用服务->应用服务日志中,配置“Application Logging(blob)”,截图如下:
3.运行webjob后,导航到步骤2中配置的blob存储,您可以看到生成了一个新的日志文件,并且所有日志(包括通过ILogger生成的日志)都在该文件中。
关于azure - ASP.NET Core Azure WebJob 未记录到 Azure 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60743835/