首页 > 解决方案 > Asp.Net Core WebAPI 使用没有 RegionEndpoint 或 ServiceURL 配置错误的 AWS CloudWatch Log

问题描述

当我尝试在 Azure App Service 中部署我的 Asp.Net Core WebApi 时,出现以下错误。我的 WebApi 使用Serilog.Sinks.AwsCloudWatch.

Exception Info: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured
   at Amazon.Runtime.ClientConfig.Validate()
   at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config)
   at Amazon.CloudWatchLogs.AmazonCloudWatchLogsClient..ctor()
   at Serilog.Sinks.AwsCloudWatch.AwsCloudWatchConfigurationExtension.AmazonCloudWatch(LoggerSinkConfiguration loggerConfiguration, String logGroup, ILogStreamNameProvider logStreamNameProvider, LogEventLevel restrictedToMinimumLevel, Int32 batchSizeLimit, Int32 batchUploadPeriodInSeconds, Boolean createLogGroup, Int32 queueSizeLimit, Byte maxRetryAttempts, LogGroupRetentionPolicy logGroupRetentionPolicy, ITextFormatter textFormatter, IAmazonCloudWatchLogs cloudWatchClient)
   at Serilog.Sinks.AwsCloudWatch.AwsCloudWatchConfigurationExtension.AmazonCloudWatch(LoggerSinkConfiguration loggerConfiguration, String logGroup, String logStreamPrefix, LogEventLevel restrictedToMinimumLevel, Int32 batchSizeLimit, Int32 batchUploadPeriodInSeconds, Boolean createLogGroup, Int32 queueSizeLimit, Byte maxRetryAttempts, LogGroupRetentionPolicy logGroupRetentionPolicy, Boolean appendUniqueInstanceGuid, Boolean appendHostName, ITextFormatter textFormatter, IAmazonCloudWatchLogs cloudWatchClient)
   --- End of inner exception stack trace ---

appSettings.json 中的配置是

"Serilog": {
    "Using": [ "Serilog.Sinks.AwsCloudWatch" ],
    "Region": "ap-southeast-2",
    "WriteTo": [
      {
        "Name": "AmazonCloudWatch",
        "Args": {
          "logGroup": "test-logger",
          "logStreamPrefix": "development",
          "restrictedToMinimumLevel": "Error"
        }
      }
    ]
  }

AWS 凭证被检索为

    public class RegisterAwsCredentials
    {
        public static void Register()
        {
            var options = new CredentialProfileOptions
            {
                AccessKey = Environment.GetEnvironmentVariable("AWS.Key"),
                SecretKey = Environment.GetEnvironmentVariable("AWS.Secret"),
            };
            var profile = new CredentialProfile("basic_profile", options);
            profile.Region = RegionEndpoint.APSoutheast2;
            var netSDKFile = new SharedCredentialsFile();
            netSDKFile.RegisterProfile(profile);
        }
    }

当我在本地运行代码时,它工作正常。仅当我尝试将其部署到 Azure 中的应用服务时才会发生。

任何人都可以给一些建议吗?谢谢

标签: c#amazon-cloudwatch

解决方案


推荐阅读