![](/img/trans.png)
[英]Can Azure Function v1.x be updated to use Microsoft.Azure.WebJobs 3.x?
[英]Azure WebJobs (3.x) Continuous job not showing Functions in Dashboard
我們有一個 Azure WebJob (3.x) 在 Azure 中的 API 應用程序下運行,全部為 Core 2.1。 它可以正常發布並運行,但不顯示任何函數或在儀表板上列出函數調用。 這很奇怪,因為作業的控制台輸出確實顯示它檢測到一個函數:
[10/17/2018 09:26:19 > fa7c81: SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[10/17/2018 09:26:19 > fa7c81: SYS INFO] Status changed to Running
[10/17/2018 09:26:19 > fa7c81: INFO]
[10/17/2018 09:26:19 > fa7c81: INFO] D:\local\Temp\jobs\continuous\SubmissionJob\43ucb4rv.ipc>dotnet SubmissionJob.dll
[10/17/2018 09:26:21 > fa7c81: INFO] dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
[10/17/2018 09:26:21 > fa7c81: INFO] Hosting starting
[10/17/2018 09:26:21 > fa7c81: INFO] info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
[10/17/2018 09:26:21 > fa7c81: INFO] Starting JobHost
[10/17/2018 09:26:21 > fa7c81: INFO] info: Host.Startup[0]
[10/17/2018 09:26:21 > fa7c81: INFO] Found the following functions:
[10/17/2018 09:26:21 > fa7c81: INFO] SubmissionJob.Functions.ProcessQueueMessageAsync
[10/17/2018 09:26:21 > fa7c81: INFO]
[10/17/2018 09:26:21 > fa7c81: INFO] Application started. Press Ctrl+C to shut down.
[10/17/2018 09:26:21 > fa7c81: INFO] Hosting environment: QA
Program.cs Program
類如下所示:
public static async Task Main(string[] args)
{
var builder = new HostBuilder()
.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices()
.AddAzureStorage()
.AddServiceBus()
.AddEventHubs();
})
.ConfigureAppConfiguration(b =>
{
// Adding command line as a configuration source
b.AddCommandLine(args);
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
// If this key exists in any config, use it to enable App Insights
var appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
}
})
.ConfigureServices((context, services) =>
{
services.AddAutoMapper();
services.AddMemoryCache();
services.AddDbContext<SubmissionsDbContext>(opts =>
opts.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")));
// cloud services
services.AddTransient(s =>
CloudStorageAccount.Parse(
context.Configuration.GetConnectionString("AzureQueueConnectionString")));
services.AddTransient<IBlobReadService, AzureBlobReadService>();
services.AddSingleton<IBlobWriteService, AzureBlobWriteService>();
services.AddSingleton<IQueueWriteService, AzureQueueWriteService>();
// submission services
services.AddScoped<ISubmissionStatusService, SubmissionStatusService>();
services.AddSingleton<Functions, Functions>();
// job activator, required in webjobs sdk 3+
services.AddSingleton<IJobActivator>(new WebJobsActivator(services.BuildServiceProvider()));
})
.UseConsoleLifetime();;
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
Functions.cs 有一個具有以下簽名的方法:
public async Task ProcessQueueMessageAsync([QueueTrigger("operations")] CloudQueueMessage incomingMessage, TextWriter log)
...scm.azurewebsites.net/azurejobs/#/jobs/continuous/SubmissionJob 顯示
Continuous WebJob Details SubmissionJob
Running
Run command: run.cmd
但是它下面沒有函數調用列表,並且該作業永久保持在Running
狀態。 如果我轉到 Kudu 中的“函數”鏈接,它會顯示沒有要顯示的函數/函數調用。
有什么想法嗎?
盡管應用程序構建器明顯不同,但大部分在 Framework 4.7 中運行良好。
答案是雙重的。
您可以使用以下命令寫入 Kudu 儀表板
var builder = new HostBuilder()
.ConfigureWebJobs(b =>
{
b.AddDashboardLogging();
});
這將起作用並顯示 WebJobs 1.x、2.x 的函數調用。 但是,從 WebJobs SDK 3.x 開始,這已經過時了。 控制台輸出將繼續顯示在 Kudu 儀表板中,但不會檢測和顯示函數,也不會顯示它們的調用。 建議改為使用 Application Insights。
var builder = new HostBuilder()
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
// If this key exists in any config, use it to enable App Insights.
// This may already be configured in Azure Portal if running WebJob udner existing app with App Insights.
var appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
}
});
確保已使用存儲連接字符串配置名為AzureWebJobsStorage
的連接字符串。
另見: https : //github.com/Azure/azure-webjobs-sdk/wiki/Application-Insights-Integration
以及: https : //docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started#add-application-insights-logging
所以我只是“升級”到 Microsoft.Azure.WebJobs v3.0.14 以保持我們的 NuGet 包“最新”,但我遇到了同樣的問題。
我一直在使用早期版本,該版本使用 JobHOst 對象的“Call”方法創建觸發 Web 作業的過程要簡單得多,該方法基本上為您設置了所有儀表板日志記錄。
我使用的過程實際上是 2 行代碼來初始化和觸發 web 作業,這也神奇地為您設置了儀表板日志記錄。
在“升級”並確定使用“CallAsync”方法觸發 Web 作業的新過程后,我發現“頂級”儀表板日志中的所有日志都消失了。 'Function' 數據不再報告,我無法再驗證我的網絡作業為調試做了什么。 我只能看到“成功”或“失敗”的信息。
我花了幾個小時試圖弄清楚如何在 HostBuilder ConfigwebJobs 和 ConfigureLogging 部分中使用 AddDashboardLogging 方法,但這些設置似乎沒有影響。
經過多次反復試驗,我終於發現了如何取回調試數據。
基本上,他們改變了儀表板日志記錄的工作方式。 您根本不使用 AddDashboardLogging 方法。 相反,您只需使用 AddConsole 方法。 這不會帶回您可能習慣的“功能”鏈接,但它確實確保您的所有常規“日志”輸出都發布到“頂級”儀表板日志。 因此,無需導航到“功能”然后選擇“切換輸出”,您只需從用於包含最少數據的頂級儀表板視圖中選擇“切換輸出”。
這是對我有用的:
首先確保您擁有所有這 3 個 using 語句,如果您不能將它們全部添加,則您可能缺少 NuGet。
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
接下來創建您的主機生成器並使用“ConfigureLogging(Logger)”方法(不是具有 2 個輸入的方法)並為 Logger 對象調用 AddConsole 方法。
這是我的最小示例代碼:
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
})
.ConfigureLogging((logger) =>
{
logger.AddConsole();
});
同樣,這不會帶回“功能”鏈接,但它確實讓我的調試日志消息打印到“頂級”網絡作業日志。 (另請注意,它不需要您調用過時的 AddDashboardLogging 方法)
默認情況下不啟用儀表板日志記錄。
要啟用該功能,請在您正在配置的 webjobs 構建器實例上調用AddDashboardLogging
擴展方法
您是否錯過了以下代碼
Functions 是函數類的名稱。
.ConfigureServices((hostBuilderContext, services) =>
{
services.AddScoped<Functions, Functions>();
})
我認為這應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.