예제 #1
0
        public void CheckEnumValue_Defined()
        {
            var parameterName = "parameterName";
            var value         = SampleEnum.DefinedValue;

            Assert.Equal(value, GaxPreconditions.CheckEnumValue(value, nameof(parameterName)));
        }
예제 #2
0
        internal void ModifyRequest(PatchRequest request)
        {
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (IfMetagenerationMatch != null)
            {
                request.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (PredefinedAcl != null)
            {
                request.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (PredefinedDefaultObjectAcl != null)
            {
                request.PredefinedDefaultObjectAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedDefaultObjectAclEnum)PredefinedDefaultObjectAcl, nameof(PredefinedDefaultObjectAcl));
            }
            if (UserProject != null)
            {
                request.UserProject = UserProject;
            }
        }
 /// <summary>
 /// Modifies the specified request for all non-null properties of this options object.
 /// </summary>
 /// <param name="request">The request to modify</param>
 internal void ModifyRequest(ListRequest request)
 {
     if (PageSize != null)
     {
         request.MaxResults = PageSize;
     }
     if (Delimiter != null)
     {
         request.Delimiter = Delimiter;
     }
     if (IncludeTrailingDelimiter != null)
     {
         request.IncludeTrailingDelimiter = IncludeTrailingDelimiter;
     }
     if (Versions != null)
     {
         request.Versions = Versions;
     }
     if (Projection != null)
     {
         request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
     }
     if (UserProject != null)
     {
         request.UserProject = UserProject;
     }
     if (PageToken != null)
     {
         request.PageToken = PageToken;
     }
 }
예제 #4
0
        public void CheckEnumValue_NotDefined()
        {
            var parameterName = "parameterName";
            var exception     = Assert.Throws <ArgumentException>(() => GaxPreconditions.CheckEnumValue((SampleEnum)5, nameof(parameterName)));

            Assert.Equal(parameterName, exception.ParamName);
        }
            private Options(
                TimeSpan?duration, DateTimeOffset?expiration, SigningVersion version, UrlStyle?urlStyle, string scheme, string bucketBoundHostname)
            {
                GaxPreconditions.CheckArgument(
                    duration.HasValue != expiration.HasValue,
                    nameof(duration),
                    "One and only one of {0} or {1} must be specified",
                    nameof(duration),
                    nameof(expiration));
                GaxPreconditions.CheckArgument(
                    (bucketBoundHostname == null && urlStyle != UrlStyle.BucketBoundHostname) ||
                    (bucketBoundHostname != null && urlStyle == UrlStyle.BucketBoundHostname),
                    nameof(bucketBoundHostname),
                    $"For using a bucket bound domain to generate the signed URL, please use the {nameof(WithBucketBoundHostname)} method.");
                if (scheme != null)
                {
                    scheme = scheme.ToLowerInvariant();
                    GaxPreconditions.CheckArgument(
                        scheme.Equals("http") || scheme.Equals("https"),
                        nameof(scheme),
                        "Only http and https are supported.");
                }
                if (urlStyle.HasValue)
                {
                    GaxPreconditions.CheckEnumValue(urlStyle.Value, nameof(urlStyle));
                }

                Duration            = duration;
                Expiration          = expiration;
                SigningVersion      = version;
                UrlStyle            = urlStyle ?? UrlStyle.PathStyle;
                Scheme              = scheme ?? "https";
                BucketBoundHostname = bucketBoundHostname;
            }
        internal void ModifyRequest(UpdateRequest request, Object obj)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }
            if (ForceNoPreconditions == true && AnyExplicitPreconditions)
            {
                throw new ArgumentException($"Cannot specify {nameof(ForceNoPreconditions)} and any explicit precondition in the same options", "options");
            }

            if (Generation != null)
            {
                request.Generation = Generation;
            }
            if (ForceNoPreconditions != true && !AnyExplicitPreconditions)
            {
                request.IfMetagenerationMatch = obj.Metageneration;
                request.IfGenerationMatch     = obj.Generation;
            }
            else
            {
                if (IfGenerationMatch != null)
                {
                    request.IfGenerationMatch = IfGenerationMatch;
                }
                if (IfGenerationNotMatch != null)
                {
                    request.IfGenerationNotMatch = IfGenerationNotMatch;
                }
                if (IfMetagenerationMatch != null)
                {
                    request.IfMetagenerationMatch = IfMetagenerationMatch;
                }
                if (IfMetagenerationNotMatch != null)
                {
                    request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
                }
            }
            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (PredefinedAcl != null)
            {
                request.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (UserProject != null)
            {
                request.UserProject = UserProject;
            }
        }
예제 #7
0
 /// <summary>
 /// Constructs a new instance of the <see cref="TopicNameOneof"/> resource name class
 /// from a suitable <see cref="IResourceName"/> instance.
 /// </summary>
 public TopicNameOneof(OneofType type, IResourceName name)
 {
     Type = GaxPreconditions.CheckEnumValue <OneofType>(type, nameof(type));
     Name = GaxPreconditions.CheckNotNull(name, nameof(name));
     if (!IsValid(type, name))
     {
         throw new ArgumentException($"Mismatched OneofType '{type}' and resource name '{name}'");
     }
 }
        internal void ModifyMediaUpload(CustomMediaUpload upload)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (ChunkSize != null)
            {
                upload.ChunkSize = ChunkSize.Value;
            }
            if (PredefinedAcl != null)
            {
                upload.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (IfGenerationMatch != null)
            {
                upload.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                upload.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                upload.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                upload.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                upload.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (UserProject != null)
            {
                upload.UserProject = UserProject;
            }

            // Note: specifying this and EncryptionKey as non-null/non-None is invalid, but that's checked in StorageClientImpl.
            if (KmsKeyName != null)
            {
                upload.KmsKeyName = KmsKeyName;
            }
            if (Origin != null)
            {
                upload.Options.ModifySessionInitiationRequest += message => message.Headers.Add("Origin", Origin);
            }
        }
 private LoggerOptions(
     LogLevel logLevel, Dictionary <string, string> labels,
     MonitoredResource monitoredResource, BufferOptions bufferOptions)
 {
     LogLevel          = GaxPreconditions.CheckEnumValue(logLevel, nameof(logLevel));
     Labels            = labels;
     MonitoredResource = monitoredResource;
     BufferOptions     = bufferOptions;
 }
 /// <summary>
 /// Constructs a parameter with the given name, type and value.
 /// </summary>
 /// <param name="name">The initial name of the parameter.</param>
 /// <param name="type">The initial type of the parameter.</param>
 /// <param name="value">The initial value of the parameter.</param>
 public BigqueryParameter(string name, BigqueryParameterType?type, object value)
 {
     Name = name;
     if (type != null)
     {
         Type = GaxPreconditions.CheckEnumValue(type.Value, nameof(type));
     }
     ValidateValue(value, nameof(value)); // Proof against refactoring of parameter name...
     Value = value;
 }
        private MediaDownloader CreateDownloader(DownloadObjectOptions options)
        {
            DownloadValidationMode mode = options?.DownloadValidationMode ?? DownloadValidationMode.Always;

            GaxPreconditions.CheckEnumValue(mode, nameof(DownloadObjectOptions.DownloadValidationMode));

            MediaDownloader downloader = mode == DownloadValidationMode.Never
                ? new MediaDownloader(Service) : new HashValidatingDownloader(Service);

            options?.ModifyDownloader(downloader);
            ApplyEncryptionKey(options?.EncryptionKey, downloader);
            return(downloader);
        }
예제 #12
0
 internal RetryOptions(RetryType retryType, ExceptionHandling exceptionHandling, BufferOverflow bufferOverflow,
                       int?bufferSizeBytes = null, int?retryAttempts = null, TimeSpan?retryInterval = null)
 {
     RetryType         = GaxPreconditions.CheckEnumValue(retryType, nameof(retryType));
     ExceptionHandling = GaxPreconditions.CheckEnumValue(exceptionHandling, nameof(exceptionHandling));
     BufferOverflow    = GaxPreconditions.CheckEnumValue(bufferOverflow, nameof(bufferOverflow));
     BufferSizeBytes   = GaxPreconditions.CheckArgumentRange(
         bufferSizeBytes ?? 0, nameof(bufferSizeBytes), 0, int.MaxValue);
     RetryAttempts = GaxPreconditions.CheckArgumentRange(
         retryAttempts ?? 0, nameof(retryAttempts), 0, int.MaxValue);
     RetryInterval = retryInterval ?? TimeSpan.Zero;
     GaxPreconditions.CheckArgument(RetryInterval >= TimeSpan.Zero, nameof(retryInterval),
                                    $"{nameof(retryInterval)} must be greater than 0");
 }
예제 #13
0
 /// <summary>
 /// Modifies the specified request for all non-null properties of this options object.
 /// </summary>
 /// <param name="request">The request to modify</param>
 internal void ModifyRequest(ListRequest request)
 {
     if (Prefix != null)
     {
         request.Prefix = Prefix;
     }
     if (PageSize != null)
     {
         request.MaxResults = PageSize;
     }
     if (Projection != null)
     {
         request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
     }
 }
예제 #14
0
        internal void ModifyMediaUpload(InsertMediaUpload upload)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (ChunkSize != null)
            {
                upload.ChunkSize = ChunkSize.Value;
            }
            if (PredefinedAcl != null)
            {
                upload.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (IfGenerationMatch != null)
            {
                upload.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                upload.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                upload.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                upload.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                upload.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (UserProject != null)
            {
                upload.UserProject = UserProject;
            }
        }
예제 #15
0
        internal void ModifyRequest(PatchRequest request)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (Generation != null)
            {
                request.Generation = Generation;
            }
            if (IfGenerationMatch != null)
            {
                request.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                request.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                request.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (PredefinedAcl != null)
            {
                request.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (UserProject != null)
            {
                request.UserProject = UserProject;
            }
        }
 internal void ModifyRequest(BucketsResource.InsertRequest request)
 {
     if (PredefinedAcl != null)
     {
         request.PredefinedAcl =
             GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
     }
     if (PredefinedDefaultObjectAcl != null)
     {
         request.PredefinedDefaultObjectAcl =
             GaxPreconditions.CheckEnumValue((PredefinedDefaultObjectAclEnum)PredefinedDefaultObjectAcl, nameof(PredefinedDefaultObjectAcl));
     }
     if (Projection != null)
     {
         request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
     }
 }
예제 #17
0
 private LoggerOptions(
     LogLevel logLevel,
     string logName,
     Dictionary <string, string> labels,
     MonitoredResource monitoredResource,
     BufferOptions bufferOptions,
     RetryOptions retryOptions,
     TextWriter loggerDiagnosticsOutput)
 {
     LogName                 = logName;
     LogLevel                = GaxPreconditions.CheckEnumValue(logLevel, nameof(logLevel));
     Labels                  = labels;
     MonitoredResource       = monitoredResource;
     BufferOptions           = bufferOptions;
     RetryOptions            = retryOptions;
     LoggerDiagnosticsOutput = loggerDiagnosticsOutput;
 }
예제 #18
0
        internal void ModifyRequest(UpdateRequest request, Bucket bucket)
        {
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }
            if (ForceNoPreconditions == true && AnyExplicitPreconditions)
            {
                throw new ArgumentException($"Cannot specify {nameof(ForceNoPreconditions)} and any explicit precondition in the same options", "options");
            }

            if (ForceNoPreconditions != true && !AnyExplicitPreconditions)
            {
                request.IfMetagenerationMatch = bucket.Metageneration;
            }
            else
            {
                if (IfMetagenerationMatch != null)
                {
                    request.IfMetagenerationMatch = IfMetagenerationMatch;
                }
                if (IfMetagenerationNotMatch != null)
                {
                    request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
                }
            }
            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (PredefinedAcl != null)
            {
                request.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (PredefinedDefaultObjectAcl != null)
            {
                request.PredefinedDefaultObjectAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedDefaultObjectAclEnum)PredefinedDefaultObjectAcl, nameof(PredefinedDefaultObjectAcl));
            }
            if (UserProject != null)
            {
                request.UserProject = UserProject;
            }
        }
 /// <summary>
 /// Modifies the specified request for all non-null properties of this options object.
 /// </summary>
 /// <param name="request">The request to modify</param>
 internal void ModifyRequest(ListRequest request)
 {
     if (PageSize != null)
     {
         request.MaxResults = PageSize;
     }
     if (Delimiter != null)
     {
         request.Delimiter = Delimiter;
     }
     if (Versions != null)
     {
         request.Versions = Versions;
     }
     if (Projection != null)
     {
         request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
     }
 }
        internal void ModifyRequest(GetRequest request)
        {
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (IfMetagenerationMatch != null)
            {
                request.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
        }
예제 #21
0
        private LoggerOptions(
            LogLevel logLevel,
            string logName,
            Dictionary <string, string> labels,
            MonitoredResource monitoredResource,
            BufferOptions bufferOptions,
            RetryOptions retryOptions,
            TextWriter loggerDiagnosticsOutput,
            string serviceName,
            string version)
        {
            LogName                 = logName;
            LogLevel                = GaxPreconditions.CheckEnumValue(logLevel, nameof(logLevel));
            Labels                  = labels;
            MonitoredResource       = monitoredResource;
            BufferOptions           = bufferOptions;
            RetryOptions            = retryOptions;
            LoggerDiagnosticsOutput = loggerDiagnosticsOutput;

            // Create the service context here, this class is inmutable.
            ServiceContext = CreateServiceContext(serviceName, version);
        }
예제 #22
0
            internal UploadHelper(
                StorageClient client,
                Object destination,
                Stream source,
                UploadObjectOptions options,
                IProgress <IUploadProgress> progress)
            {
                _client      = client;
                _mediaUpload = client.CreateObjectUploader(destination, source, options);
                if (progress != null)
                {
                    _mediaUpload.ProgressChanged += progress.Report;
                }

                var validationMode = options?.UploadValidationMode ?? UploadObjectOptions.DefaultValidationMode;

                GaxPreconditions.CheckEnumValue(validationMode, nameof(UploadObjectOptions.UploadValidationMode));
                switch (validationMode)
                {
                case UploadValidationMode.DeleteAndThrow:
                    _crc = new Crc32c();
                    _mediaUpload.UploadStreamInterceptor += _crc.UpdateHash;
                    _validationFailureAction              = obj => client.DeleteObject(obj, new DeleteObjectOptions {
                        Generation = obj.Generation
                    });
                    _validationFailureAsyncAction = (obj, token) => client.DeleteObjectAsync(obj, new DeleteObjectOptions {
                        Generation = obj.Generation
                    }, token);
                    break;

                case UploadValidationMode.ThrowOnly:
                    _crc = new Crc32c();
                    _mediaUpload.UploadStreamInterceptor += _crc.UpdateHash;
                    break;
                }
            }
예제 #23
0
 private LogTarget(LogTargetKind kind, string projectId, string organizationId)
 {
     Kind           = GaxPreconditions.CheckEnumValue(kind, nameof(kind));
     ProjectId      = projectId;
     OrganizationId = organizationId;
 }
예제 #24
0
        /// <inheritdoc/>
        public override void ActivateOptions()
        {
            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? LoggingServiceV2Client.Create();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;
            _platform  = _platform ?? Platform.Instance();

            // Normalize string configuration
            ResourceType = string.IsNullOrWhiteSpace(ResourceType) ? null : ResourceType;
            ProjectId    = string.IsNullOrWhiteSpace(ProjectId) ? null : ProjectId;
            LogId        = string.IsNullOrWhiteSpace(LogId) ? null : LogId;

            // Validate configuration
            GaxPreconditions.CheckState(LogId != null, $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxMemorySize)} must be configured to be > 0");
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            ActivateLogIdAndResource();
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            _initIdTask = Task.Run(_logQ.GetPreviousExecutionIdAsync);
            var labels = new Dictionary <string, string>();

            foreach (var customLabel in _customLabels)
            {
                labels.Add(customLabel.Key, customLabel.Value);
            }
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                Labels      = { _customLabels.ToDictionary(x => x.Key, x => x.Value) },
            };
            var serverErrorBackoffSettings = new BackoffSettings(
                delay: TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                delayMultiplier: ServerErrorBackoffMultiplier,
                maxDelay: TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds)
                );

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorBackoffSettings);
            _isActivated = true;
        }
 /// <summary>
 /// Creates a new instance of this type with the specified emulator detection value.
 /// </summary>
 /// <param name="emulatorDetection">Determines how and whether to detect the emulator.</param>
 /// <returns>The new instance</returns>
 public ClientCreationSettings WithEmulatorDetection(EmulatorDetection emulatorDetection)
 {
     GaxPreconditions.CheckEnumValue(emulatorDetection, nameof(emulatorDetection));
     return(new ClientCreationSettings(ClientCount, PublisherServiceApiSettings, Credentials, ServiceEndpoint, emulatorDetection));
 }
 private LoggerOptions(LogLevel logLevel, MonitoredResource monitoredResource, BufferOptions bufferOptions)
 {
     LogLevel          = GaxPreconditions.CheckEnumValue(logLevel, nameof(logLevel));
     MonitoredResource = monitoredResource;
     BufferOptions     = bufferOptions;
 }
예제 #27
0
        internal void ModifyRequest(RewriteRequest request)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }
            if (IfSourceGenerationMatch != null && IfSourceGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfSourceGenerationMatch)} and {nameof(IfSourceGenerationNotMatch)} in the same options", "options");
            }
            if (IfSourceMetagenerationMatch != null && IfSourceMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfSourceMetagenerationMatch)} and {nameof(IfSourceMetagenerationNotMatch)} in the same options", "options");
            }

            if (DestinationPredefinedAcl != null)
            {
                request.DestinationPredefinedAcl =
                    GaxPreconditions.CheckEnumValue((DestinationPredefinedAclEnum)DestinationPredefinedAcl, nameof(DestinationPredefinedAcl));
            }
            if (SourceGeneration != null)
            {
                request.SourceGeneration = SourceGeneration;
            }
            if (Projection != null)
            {
                request.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }

            if (IfGenerationMatch != null)
            {
                request.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                request.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                request.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                request.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }

            if (IfSourceGenerationMatch != null)
            {
                request.IfSourceGenerationMatch = IfSourceGenerationMatch;
            }
            if (IfSourceGenerationNotMatch != null)
            {
                request.IfSourceGenerationNotMatch = IfSourceGenerationNotMatch;
            }
            if (IfSourceMetagenerationMatch != null)
            {
                request.IfSourceMetagenerationMatch = IfSourceMetagenerationMatch;
            }
            if (IfSourceMetagenerationNotMatch != null)
            {
                request.IfSourceMetagenerationNotMatch = IfSourceMetagenerationNotMatch;
            }
            if (UserProject != null)
            {
                request.UserProject = UserProject;
            }

            // Note: specifying this and EncryptionKey as non-null/non-None is invalid, but that's checked in StorageClientImpl.
            if (KmsKeyName != null)
            {
                request.DestinationKmsKeyName = KmsKeyName;
            }
        }
예제 #28
0
 private LoggerOptions(LogLevel logLevel, BufferOptions bufferOptions)
 {
     LogLevel      = GaxPreconditions.CheckEnumValue(logLevel, nameof(logLevel));
     BufferOptions = GaxPreconditions.CheckNotNull(bufferOptions, nameof(bufferOptions));
 }
        /// <inheritdoc/>
        public override void ActivateOptions()
        {
            // Validate configuration
            GaxPreconditions.CheckState(string.IsNullOrWhiteSpace(CredentialFile) || string.IsNullOrWhiteSpace(CredentialJson),
                                        $"{nameof(CredentialFile)} and {nameof(CredentialJson)} must not both be set.");
            GaxPreconditions.CheckState(LogId != null, $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));

            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? BuildLoggingServiceClient();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;
            _platform  = _platform ?? Platform.Instance();

            // Normalize string configuration
            ResourceType = string.IsNullOrWhiteSpace(ResourceType) ? null : ResourceType;
            ProjectId    = string.IsNullOrWhiteSpace(ProjectId) ? null : ProjectId;
            LogId        = string.IsNullOrWhiteSpace(LogId) ? null : LogId;

            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxMemorySize)} must be configured to be > 0");
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            ActivateLogIdAndResource();
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            _initIdTask = Task.Run(_logQ.GetPreviousExecutionIdAsync);
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                // Patterns included in custom labels will not be used in this "logs lost" entry.
                // The pattern itself will be logged, regardless of the "UsePatternWithinCustomLabels" setting.
                // This is acceptable as most patterns will be irrelevant in this context.
                Labels = { _customLabels.ToDictionary(x => x.Key, x => x.Value) },
            };
            var serverErrorRetrySettings = RetrySettings.FromExponentialBackoff(maxAttempts: int.MaxValue,
                                                                                initialBackoff: TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                                                                                maxBackoff: TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds),
                                                                                backoffMultiplier: ServerErrorBackoffMultiplier,
                                                                                retryFilter: _ => true); // Ignored

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorRetrySettings);
            if (_usePatternWithinCustomLabels)
            {
                // Initialize a pattern layout for each custom label.
                _customLabelsPatterns = _customLabels.Select(x => new CustomLabelPattern(x.Key, new PatternLayout(x.Value))).ToArray();
            }
            _isActivated = true;
        }
        public override void ActivateOptions()
        {
            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? LoggingServiceV2Client.Create();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;

            // Validate configuration
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(ResourceType), $"{nameof(ResourceType)} must be set.");
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(ProjectId), $"{nameof(ProjectId)} must be set.");
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(LogId), $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxFileSize)} must be configured to be > 0");
                break;

            case LocalQueueType.Disk:
                GaxPreconditions.CheckState(!string.IsNullOrEmpty(File), $"{nameof(File)} must be set.");
                GaxPreconditions.CheckState(MaxFileSize > 0, $"{nameof(MaxFileSize)} must be > 0");
                GaxPreconditions.CheckState(MaxSizeRollBackups > 0, $"{nameof(MaxSizeRollBackups)} must be > 0");
                break;

            default:
                throw new InvalidOperationException("Inconceivable!");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            // Configure the logger from the given configuration
            _logName  = LoggingServiceV2Client.FormatLogName(ProjectId, LogId);
            _resource = new MonitoredResource
            {
                Type = ResourceType
            };
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            case LocalQueueType.Disk:
                throw new NotImplementedException("File-base local queues not implemented.");

            default:
                throw new InvalidOperationException("Inconceivable!");
            }
            _initNextIdTask = Task.Run(InitNextId);
            var labels = new Dictionary <string, string>();

            foreach (var customLabel in _customLabels)
            {
                labels.Add(customLabel.Key, customLabel.Value);
            }
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                Labels      = { labels },
            };
            var serverErrorBackoffSettings = new BackoffSettings
            {
                Delay           = TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                DelayMultiplier = ServerErrorBackoffMultiplier,
                MaxDelay        = TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds),
            };

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorBackoffSettings);
        }