Azure功能:应用程序设置在生产环境中不起作用

bbmckpt7  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(139)

我在local.settings.json中有以下值
x1c 0d1x的数据
我在我的启动类中访问相同的,我为我的Azure函数添加了如下内容

public override void Configure(IFunctionsHostBuilder builder)
    {
        IConfiguration config;
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddEnvironmentVariables()
            .AddJsonFile($"local.settings.json", optional: true, reloadOnChange: true)
            .Build();

        builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), config));
        builder.Services.AddLogging();
        var executionContextOptions = builder.Services.BuildServiceProvider()
        .GetService<IOptions<ExecutionContextOptions>>().Value;
        var currentDirectory = executionContextOptions.AppDirectory;


        builder.Services.AddSingleton<IConfiguration>(config);

        #region Serilog
        Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
        .MinimumLevel.Override("Worker", LogEventLevel.Warning)
        .MinimumLevel.Override("Host", LogEventLevel.Warning)
        .MinimumLevel.Override("System", LogEventLevel.Error)
        .MinimumLevel.Override("Function", LogEventLevel.Error)
        .MinimumLevel.Override("DurableTask", LogEventLevel.Error)
        .MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Error)
        .MinimumLevel.Override("Azure.Core", LogEventLevel.Error)
        .MinimumLevel.Override("Azure.Messaging.ServiceBus", LogEventLevel.Error)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .WriteTo.AzureAnalytics(config["AZURE_WORKSPACE_ID"],///<-***issue here///
         config["AZURE_AUTHENTICATION_ID"])
        .Enrich.WithEnvironmentName()
        .CreateLogger();
        builder.Services.AddLogging(lb => lb.AddSerilog(Log.Logger, true));
        #endregion

        
    }

字符串
它在本地工作正常,但在生产环境中,即使我在应用程序设置中有AZURE_WORKSPACE_ID键,



我得到以下错误
Microsoft.Azure.WebJobs.Script.ExternalStartupException:在外部启动类中配置服务时出错。-> System.ArgumentNullException:值不能为空。(参数“workspaceId”)在Serilog. LoggerOperationExtensions.AzureAnalytics(LoggerSinkConfiguration loggerConfiguration,String workspaceId,String authenticationId,String logName,LogEventLevel restrictedToMinimumLevel,Boolean storeTimestampInUtc,IstampProvider formatProvider,Int32 logBufferSize,Int32 batchSize,AzureOfferingType azureOfferingType、LoggingLevelSwitch levelSwitch、Boolean对象、String代理)
AZURE_WORKSPACE_ID值未被拾取

drkbr07n

drkbr07n1#

这段代码在本地和Azure上都适用,如下所示:
我在.NET 7 isolated中创建了一个HTTP触发器函数,它从本地的local.settings.json和Azure的app settings中获取自定义环境变量。

Program.cs

using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()  
.ConfigureFunctionsWorkerDefaults()  
.Build();

var config = new ConfigurationBuilder()  
.SetBasePath(Environment.CurrentDirectory)  
.AddEnvironmentVariables()  
.AddJsonFile($"local.settings.json",optional: true, reloadOnChange:true)  
.Build();

host.Run();

字符串

Function1.cs

using System.Net;  
using Microsoft.Azure.Functions.Worker;  
using Microsoft.Azure.Functions.Worker.Http;  
using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.Logging;

namespace FunctionApp5  
{  
public class Function1  
{  
private readonly ILogger _logger;  
private readonly IConfiguration _config;

public Function1(IConfiguration configuration, ILoggerFactory loggerFactory)  
{  
_logger = loggerFactory.CreateLogger<Function1>();  
_config = configuration;  
}

[Function("Function1")]  
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)  
{  
_logger.LogInformation("C# HTTP trigger function processed a request.");

string value = _config["Azure_custom_variable"];

var response = req.CreateResponse(HttpStatusCode.OK);  
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

response.WriteString($"Welcome to Azure Functions! \n\n {value}");

return response;  
}  
}  
}

本地环境

local.settings.json

{  
"IsEncrypted": false,  
"Values": {  
"AzureWebJobsStorage": "UseDevelopmentStorage=true",  
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",  
"Azure_custom_variable": "Vivek, This is a HTTP trigger"  
}  
}

Output


的数据


Azure


Output


相关问题