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

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

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

  1. public override void Configure(IFunctionsHostBuilder builder)
  2. {
  3. IConfiguration config;
  4. var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
  5. config = new ConfigurationBuilder()
  6. .SetBasePath(Directory.GetCurrentDirectory())
  7. .AddEnvironmentVariables()
  8. .AddJsonFile($"local.settings.json", optional: true, reloadOnChange: true)
  9. .Build();
  10. builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), config));
  11. builder.Services.AddLogging();
  12. var executionContextOptions = builder.Services.BuildServiceProvider()
  13. .GetService<IOptions<ExecutionContextOptions>>().Value;
  14. var currentDirectory = executionContextOptions.AppDirectory;
  15. builder.Services.AddSingleton<IConfiguration>(config);
  16. #region Serilog
  17. Log.Logger = new LoggerConfiguration()
  18. .MinimumLevel.Information()
  19. .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
  20. .MinimumLevel.Override("Worker", LogEventLevel.Warning)
  21. .MinimumLevel.Override("Host", LogEventLevel.Warning)
  22. .MinimumLevel.Override("System", LogEventLevel.Error)
  23. .MinimumLevel.Override("Function", LogEventLevel.Error)
  24. .MinimumLevel.Override("DurableTask", LogEventLevel.Error)
  25. .MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Error)
  26. .MinimumLevel.Override("Azure.Core", LogEventLevel.Error)
  27. .MinimumLevel.Override("Azure.Messaging.ServiceBus", LogEventLevel.Error)
  28. .Enrich.FromLogContext()
  29. .WriteTo.Console()
  30. .WriteTo.AzureAnalytics(config["AZURE_WORKSPACE_ID"],///<-***issue here///
  31. config["AZURE_AUTHENTICATION_ID"])
  32. .Enrich.WithEnvironmentName()
  33. .CreateLogger();
  34. builder.Services.AddLogging(lb => lb.AddSerilog(Log.Logger, true));
  35. #endregion
  36. }

字符串
它在本地工作正常,但在生产环境中,即使我在应用程序设置中有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

  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.Hosting;
  3. var host = new HostBuilder()
  4. .ConfigureFunctionsWorkerDefaults()
  5. .Build();
  6. var config = new ConfigurationBuilder()
  7. .SetBasePath(Environment.CurrentDirectory)
  8. .AddEnvironmentVariables()
  9. .AddJsonFile($"local.settings.json",optional: true, reloadOnChange:true)
  10. .Build();
  11. host.Run();

字符串

Function1.cs

  1. using System.Net;
  2. using Microsoft.Azure.Functions.Worker;
  3. using Microsoft.Azure.Functions.Worker.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Logging;
  6. namespace FunctionApp5
  7. {
  8. public class Function1
  9. {
  10. private readonly ILogger _logger;
  11. private readonly IConfiguration _config;
  12. public Function1(IConfiguration configuration, ILoggerFactory loggerFactory)
  13. {
  14. _logger = loggerFactory.CreateLogger<Function1>();
  15. _config = configuration;
  16. }
  17. [Function("Function1")]
  18. public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
  19. {
  20. _logger.LogInformation("C# HTTP trigger function processed a request.");
  21. string value = _config["Azure_custom_variable"];
  22. var response = req.CreateResponse(HttpStatusCode.OK);
  23. response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
  24. response.WriteString($"Welcome to Azure Functions! \n\n {value}");
  25. return response;
  26. }
  27. }
  28. }

本地环境

local.settings.json

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  5. "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
  6. "Azure_custom_variable": "Vivek, This is a HTTP trigger"
  7. }
  8. }

Output


的数据


Azure


Output


展开查看全部

相关问题