user1060500
user1060500

Reputation: 1535

WebJob is not writing logs

Is there anything else I need to do here in order to get my WebJob to write to the storage log? Something must be missing, but I am not sure what it could be.

Documentation is vague and only indicates that
ProcessQueueMessage function is in a class named StorageQueueProcessor and the method successfully processes new items added to the storage queue. AzureWebJobsDashboard is using the same storage queue for logs, but no logs are being written.

The following connection strings are defined in appSettings.json and they are correct:

  "ConnectionStrings": {
    "Storage": ""
    "ServiceBus": ""
    "AzureWebJobsStorage": ""
    "AzureWebJobsDashboard": ""
  },

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "Warning",
      "Microsoft": "Information"
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning",
        "System": "Information",
        "Microsoft": "Information"
      }

Here is the C# code that runs on startup:

static async Task MainAsync(string[] args)
        {
            Action<QueuesOptions> qOptions = delegate (QueuesOptions s) 
            {
                s.MaxDequeueCount = 1;
            };

            var builder = new HostBuilder()
               .ConfigureWebJobs(b =>
               {
                   b.AddAzureStorageCoreServices()
                    .AddAzureStorage();

                })
               .ConfigureAppConfiguration(b =>
               {

                })
               .ConfigureLogging((context, b) =>
               {
                   b.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug);
                   b.AddConsole();
                })
               .UseConsoleLifetime();

            var host = builder.Build();
            using (host)
            {
                await host.RunAsync();
            }

        }

Here is the code to process the storage queue item and attempt to write to azure storage logs:

 public static async Task ProcessQueueMessage([QueueTrigger("site-indexer")] CloudQueueMessage message, TextWriter log)
        {
                System.Console.Out.WriteLine($"Out");
                System.Console.Error.WriteLine($"Error Test Line");
                log.WriteLine($"Log Write Line");
   }

Upvotes: 1

Views: 1883

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

You need to install the package Microsoft.Extensions.Logging.AzureAppServices, version 2.2.5. Then in the Main method -> ConfigureLogging, add b.AddAzureWebAppDiagnostics();

The sample code:

Main method:

        //other code

        var builder = new HostBuilder()
        .ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();
        })
        .ConfigureAppConfiguration(b =>
        {

        }
        )
        .ConfigureLogging((context, b) =>
        {
            b.SetMinimumLevel(LogLevel.Debug);
            b.AddConsole();

            //add this line of code.
            b.AddAzureWebAppDiagnostics();
        })                
        .UseConsoleLifetime();

         //other code

In Function.cs:

        public static void ProcessQueueMessage([QueueTrigger("myqueue1", Connection = "AzureWebJobsStorage")]CloudQueueMessage message, ILogger log)
        {
            log.LogInformation("hello, can you see me?");
            log.LogInformation("Log test message:" + DateTime.Now.ToLongDateString());
            log.LogInformation("the queue message is: " + message.AsString);
        }

Then nav to azure portal -> web app service(the webjob is published to) -> in the left pane, click "App Service Logs" -> Enable the "Application Logging(Blob)" and configure other settings like logLevel, the blob storage. Screenshot as blow:

enter image description here

After publish the webjob to the web app, run the webjobs, and you can check the log info in the kudu site:

enter image description here

At last, nav to the blob container where you configured in the above steps, you can see the logs there:

enter image description here

Upvotes: 2

Related Questions