ASP.NET Core Azure WebJob未登录到Azure存储

问题描述 投票:0回答:2

[以前,当我在.NET中创建Azure Webjobs时,它已自动将日志记录配置为路径/azure-webjobs-hosts/output-logs/.txt上Azure存储上的文本文件。但是现在,当我使用.NET Core和Webjobs SDK的3.0.14版本时,默认情况下它不再具有此行为。

登录到控制台以及Application Insights都可以在我当前的设置下很好地工作,但是我确实希望通过绑定的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
}
asp.net-core azure-storage azure-webjobs azure-webjobssdk
2个回答
2
投票

我也希望通过绑定的ILogger在Azure存储上记录文本文件。

Serilog.Sinks.AzureBlobStorage可以帮助将日志写入Azure Blob存储,并且在Azure和本地都可以很好地工作。

在Program.cs中

builder.ConfigureLogging((context, b) =>
{
    b.AddConsole();

    var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

    var AzureBlobStorageLogger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();

    b.AddSerilog(logger: AzureBlobStorageLogger);
});

在appsettings.json

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.AzureBlobStorage"
    ],
    "WriteTo": [
      {
        "Name": "AzureBlobStorage",
        "Args": {
          "connectionString": "{your_connectionString_here}",
          "storageContainerName": "azure-webjobs-hosts",
          "storageFileName": "output-logs/{yyyy}/{MM}/{dd}/log.txt"
        }
      }
    ]
  },
  //other settings
  //...
}

此外,正如@IvanYang所提到的,您也可以通过添加Azure diagnostics logger来实现,但是此提供程序仅在项目在Azure环境中运行时有效。当我们在本地运行webjob时,它不会将日志写入Azure Blob存储。


1
投票

如果要将日志(通过ILogger)保存到Blob存储,则需要将webjobs发布为Azure。您可以按照以下步骤操作:

1。将b.AddAzureWebAppDiagnostics();方法(需要将nuget软件包Microsoft.Extensions.Logging.AzureAppServices安装到builder.ConfigureLogging代码块中:]

        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();
            b.AddAzureWebAppDiagnostics();

            //other code
        });

2。将其发布到azure,然后在azure门户中->相关的应用程序服务->应用程序服务日志,配置“ Application Logging(blob)”,屏幕截图如下:

enter image description here

3。运行webjob之后,导航到在步骤2中配置的Blob存储,您可以看到生成了一个新的日志文件,并且所有日志(包括通过ILogger记录的日志)都在该文件中。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.