Skip to content

Commit dcc93d7

Browse files
Merge pull request #293238 from ggailey777/patch-2
[WebJobs][UUF] Bug fix + freshness updates
2 parents 38dec77 + eed8d50 commit dcc93d7

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

articles/app-service/webjobs-sdk-get-started.md

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to enable your web apps to run background tasks. Use this
44
author: ggailey777
55
ms.devlang: csharp
66
ms.custom: devx-track-csharp
7-
ms.date: 06/25/2021
7+
ms.date: 01/17/2025
88
ms.author: glenga
99
ms.topic: tutorial
1010

@@ -15,7 +15,7 @@ ms.topic: tutorial
1515

1616
Get started with the Azure WebJobs SDK for Azure App Service to enable your web apps to run background tasks, scheduled tasks, and respond to events.
1717

18-
Use Visual Studio 2022 to create a .NET Core console app that uses the WebJobs SDK to respond to Azure Storage Queue messages, run the project locally, and finally deploy it to Azure.
18+
Use Visual Studio 2022 to create a .NET 8 console app that uses the WebJobs SDK to respond to Azure Storage Queue messages, run the project locally, and finally deploy it to Azure.
1919

2020
In this tutorial, you will learn how to:
2121

@@ -37,7 +37,7 @@ In this tutorial, you will learn how to:
3737
In this section, you start by creating a project in Visual Studio 2022. Next, you'll add tools for Azure development, code publishing, and functions that listen for triggers and call functions. Last, you'll set up console logging that disables a legacy monitoring tool and enables a console provider with default filtering.
3838

3939
>[!NOTE]
40-
>The procedures in this article are verified for creating a .NET Core console app that runs on .NET 6.0.
40+
>The procedures in this article are verified for creating a C# console app that runs on .NET 8.0.
4141
4242
### Create a project
4343

@@ -64,6 +64,9 @@ Install the latest WebJobs NuGet package. This package includes Microsoft.Azure.
6464
```powershell
6565
Install-Package Microsoft.Azure.WebJobs.Extensions -version <4_X_VERSION>
6666
```
67+
>[!NOTE]
68+
>The sample code in this article works with package versions 4.x. Make sure you use a 4.x version because you get build errors when using package versions 5.x.
69+
6770
5. In the **Package Manager Console**, execute the command. The extension list appears and automatically installs.
6871
6972
### Create the Host
@@ -109,10 +112,10 @@ Set up console logging that uses the [ASP.NET Core logging framework](/aspnet/co
109112
110113
1. Get the latest stable version of the [`Microsoft.Extensions.Logging.Console` NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/), which includes `Microsoft.Extensions.Logging`.
111114
112-
2. In the following command, replace `<6_X_VERSION>` with the current version number you found in step 1. Each type of NuGet Package has a unique version number.
115+
2. In the following command, replace `<9_X_VERSION>` with the current version number you found in step 1. Each type of NuGet Package has a unique version number.
113116
114117
```powershell
115-
Install-Package Microsoft.Extensions.Logging.Console -version <6_X_VERSION>
118+
Install-Package Microsoft.Extensions.Logging.Console -version <9_X_VERSION>
116119
```
117120
3. In the **Package Manager Console**, fill in the current version number and execute the command. The extension list appears and automatically installs.
118121

@@ -126,24 +129,30 @@ Set up console logging that uses the [ASP.NET Core logging framework](/aspnet/co
126129
```cs
127130
builder.ConfigureLogging((context, b) =>
128131
{
129-
b.AddConsole();
132+
b.SetMinimumLevel(LogLevel.Error);
133+
b.AddFilter("Function", LogLevel.Information);
134+
b.AddFilter("Host", LogLevel.Debug);
135+
b.AddConsole();
130136
});
131137
```
132138

133-
The `Main` method now looks like this:
139+
This adds logging that captures log output for function executions at the `Information` level, the host at the `Debug` level, and the `error` level for all other components. The `Main` method now looks like this:
134140

135141
```cs
136142
static async Task Main()
137143
{
138144
var builder = new HostBuilder();
139145
builder.ConfigureWebJobs(b =>
140-
{
141-
b.AddAzureStorageCoreServices();
142-
});
146+
{
147+
b.AddAzureStorageCoreServices();
148+
});
143149
builder.ConfigureLogging((context, b) =>
144-
{
145-
b.AddConsole();
146-
});
150+
{
151+
b.SetMinimumLevel(LogLevel.Error);
152+
b.AddFilter("Function", LogLevel.Information);
153+
b.AddFilter("Host", LogLevel.Debug);
154+
b.AddConsole();
155+
});
147156
var host = builder.Build();
148157
using (host)
149158
{
@@ -170,7 +179,7 @@ In this section, you create a function triggered by messages in an Azure Storage
170179
Starting with version 3 of the WebJobs SDK, to connect to Azure Storage services you must install a separate Storage binding extension package.
171180

172181
>[!NOTE]
173-
> Beginning with 5.x, Microsoft.Azure.WebJobs.Extensions.Storage has been [split by storage service](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Microsoft.Azure.WebJobs.Extensions.Storage/CHANGELOG.md#major-changes-and-features) and has migrated the `AddAzureStorage()` extension method by service type.
182+
> Beginning with 5.x, Microsoft.Azure.WebJobs.Extensions.Storage has been [split by storage service](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Microsoft.Azure.WebJobs.Extensions.Storage/CHANGELOG.md#major-changes-and-features) and has migrated the `AddAzureStorage()` extension method by service type. This version also requires you to update the version of the `Microsoft.Azure.WebJobs.Host.Storage` assembly used by the SDK.
174183
175184
1. Get the latest stable version of the [Microsoft.Azure.WebJobs.Extensions.Storage](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.Storage) NuGet package, version 5.x.
176185
@@ -179,8 +188,15 @@ Starting with version 3 of the WebJobs SDK, to connect to Azure Storage services
179188
```powershell
180189
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage -Version <5_X_VERSION>
181190
```
191+
182192
1. In the **Package Manager Console**, execute the command with the current version number at the `PM>` entry point.
183193

194+
1. Also run this command to update the `Microsoft.Azure.WebJobs.Host.Storage` package to version 4.1.0:
195+
196+
```powershell
197+
Install-Package Microsoft.Azure.WebJobs.Host.Storage -Version 4.1.0
198+
```
199+
184200
1. Continuing in **Program.cs**, in the `ConfigureWebJobs` extension method, add the `AddAzureStorageQueues` method on the [`HostBuilder`](/dotnet/api/microsoft.extensions.hosting.hostbuilder) instance (before the `Build` command) to initialize the Storage extension. At this point, the `ConfigureWebJobs` method looks like this:
185201

186202
```cs
@@ -205,15 +221,18 @@ Starting with version 3 of the WebJobs SDK, to connect to Azure Storage services
205221
{
206222
var builder = new HostBuilder();
207223
builder.UseEnvironment(EnvironmentName.Development);
208-
builder.ConfigureLogging((context, b) =>
209-
{
210-
b.AddConsole();
211-
});
212224
builder.ConfigureWebJobs(b =>
213225
{
214226
b.AddAzureStorageCoreServices();
215227
b.AddAzureStorageQueues();
216228
});
229+
builder.ConfigureLogging((context, b) =>
230+
{
231+
b.SetMinimumLevel(LogLevel.Error);
232+
b.AddFilter("Function", LogLevel.Information);
233+
b.AddFilter("Host", LogLevel.Debug);
234+
b.AddConsole();
235+
});
217236
var host = builder.Build();
218237
using (host)
219238
{
@@ -238,7 +257,7 @@ The `QueueTrigger` attribute tells the runtime to call this function when a new
238257

239258
namespace WebJobsSDKSample
240259
{
241-
public class Functions
260+
public static class Functions
242261
{
243262
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
244263
{
@@ -307,7 +326,7 @@ Build and run the project locally and create a message queue to trigger the func
307326

308327
5. Go back to the **Queue** window and refresh it. The message is gone, since it has been processed by your function running locally.
309328

310-
6. Close the console window.
329+
6. Close the console window or type Ctrl+C.
311330

312331
It's now time to publish your WebJobs SDK project to Azure.
313332

@@ -368,11 +387,11 @@ When the WebJob runs in Azure, you can't monitor function execution by viewing c
368387

369388
1. In your **Publish** profile page, select the three dots above **Hosting** to show **Hosting profile section actions** and choose **Open in Azure Portal**.
370389

371-
1. In the web app under **Settings**, choose **Application Insights**, and select **Turn on Application Insights**.
390+
1. In the web app under **Monitoring**, choose **Application Insights**, and select **Turn on Application Insights**.
372391

373-
1. Verify the generated **Resource name** for the instance and the **Location**, and select **Apply**.
392+
1. Verify the generated **Resource name** for the instance and the **Location**, and select **Apply** and then **Yes**.
374393

375-
1. Under **Settings**, choose **Configuration** and verify that a new `APPINSIGHTS_INSTRUMENTATIONKEY` was created. This key is used to connect your WebJob instance to Application Insights.
394+
1. Under **Settings**, choose **Environment variables** and verify that a new `APPINSIGHTS_INSTRUMENTATIONKEY` was created. This key is used to connect your WebJob instance to Application Insights.
376395

377396
To take advantage of [Application Insights](/azure/azure-monitor/app/app-insights-overview) logging, you need to update your logging code as well.
378397

@@ -406,23 +425,26 @@ The `Main` method code should now look like the following example:
406425
static async Task Main()
407426
{
408427
var builder = new HostBuilder();
409-
builder.UseEnvironment(EnvironmentName.Development);
428+
//builder.UseEnvironment(EnvironmentName.Development);
410429
builder.ConfigureWebJobs(b =>
411-
{
412-
b.AddAzureStorageCoreServices();
413-
b.AddAzureStorage();
414-
});
430+
{
431+
b.AddAzureStorageCoreServices();
432+
b.AddAzureStorageQueues();
433+
});
415434
builder.ConfigureLogging((context, b) =>
416-
{
417-
b.AddConsole();
435+
{
436+
b.SetMinimumLevel(LogLevel.Error);
437+
b.AddFilter("Function", LogLevel.Information);
438+
b.AddFilter("Host", LogLevel.Debug);
439+
b.AddConsole();
418440

419-
// If the key exists in settings, use it to enable Application Insights.
420-
string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
421-
if (!string.IsNullOrEmpty(instrumentationKey))
422-
{
423-
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
424-
}
425-
});
441+
// If the key exists in settings, use it to enable Application Insights.
442+
string? instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
443+
if (!string.IsNullOrEmpty(instrumentationKey))
444+
{
445+
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
446+
}
447+
});
426448
var host = builder.Build();
427449
using (host)
428450
{
@@ -431,7 +453,7 @@ static async Task Main()
431453
}
432454
```
433455

434-
This initializes the Application Insights logging provider with default [filtering](webjobs-sdk-how-to.md#log-filtering). When running locally, all Information and higher-level logs are written to both the console and Application Insights.
456+
This initializes the Application Insights logging provider with default [filtering](webjobs-sdk-how-to.md#log-filtering). When running locally, all Information and higher-level logs are written to both the console and Application Insights. When running locally, Application Insights logging is only supported after you also add the `APPINSIGHTS_INSTRUMENTATIONKEY` to the `appsetting.json` file in the project.
435457

436458
### Republish the project and trigger the function again
437459

@@ -441,7 +463,7 @@ This initializes the Application Insights logging provider with default [filteri
441463

442464
1. In your **Publish** profile page, select the three dots above **Hosting** to show **Hosting profile section actions** and choose **Open in Azure Portal**.
443465

444-
1. In the web app under **Settings**, choose **Application Insights**, and select **View Application Insights data**.
466+
1. In the web app under **Settings** > **Monitor**, choose **Application Insights**, and select **View Application Insights data**.
445467

446468
1. Select **Search** and then select **See all data in the last 24 hours**.
447469

0 commit comments

Comments
 (0)