/// <summary>
        /// Construct a sink that saves logs to the specified storage account.
        /// </summary>
        /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param>
        /// <param name="textFormatter"></param>
        /// <param name="batchSizeLimit"></param>
        /// <param name="period"></param>
        /// <param name="storageContainerName">Container where the log entries will be written to. Note: Optional, setting this may impact performance</param>
        /// <param name="storageFileName">File name that log entries will be written to. Note: Optional, setting this may impact performance</param>
        /// <param name="bypassBlobCreationValidation">Bypass the exception in case the blob creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="appendBlobBlockPreparer"></param>
        /// <param name="appendBlobBlockWriter"></param>
        public AzureBatchingBlobStorageSink(
            CloudStorageAccount storageAccount,
            ITextFormatter textFormatter,
            int batchSizeLimit,
            TimeSpan period,
            string storageContainerName                      = null,
            string storageFileName                           = null,
            bool bypassBlobCreationValidation                = false,
            ICloudBlobProvider cloudBlobProvider             = null,
            IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
            IAppendBlobBlockWriter appendBlobBlockWriter     = null)
            : base(batchSizeLimit, period)
        {
            this.textFormatter = textFormatter;

            if (string.IsNullOrEmpty(storageContainerName))
            {
                storageContainerName = "logs";
            }

            if (string.IsNullOrEmpty(storageFileName))
            {
                storageFileName = "log.txt";
            }

            this.storageAccount               = storageAccount;
            this.storageContainerName         = storageContainerName;
            this.blobNameFactory              = new BlobNameFactory(storageFileName);
            this.bypassBlobCreationValidation = bypassBlobCreationValidation;
            this.cloudBlobProvider            = cloudBlobProvider ?? new DefaultCloudBlobProvider();
            this.appendBlobBlockPreparer      = appendBlobBlockPreparer ?? new DefaultAppendBlobBlockPreparer();
            this.appendBlobBlockWriter        = appendBlobBlockWriter ?? new DefaultAppendBlobBlockWriter();
        }
        /// <summary>
        /// Construct a sink that saves logs to the specified storage account.
        /// </summary>
        /// <param name="cloudBlobClient">The Cloud Storage Client to use to insert the log entries to.</param>
        /// <param name="textFormatter"></param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="bypassContainerCreationValidation">Bypass the exception in case the container creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="appendBlobBlockPreparer"></param>
        /// <param name="appendBlobBlockWriter"></param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        public AzureBlobStorageSink(
            CloudBlobClient cloudBlobClient,
            ITextFormatter textFormatter,
            string storageContainerName                      = null,
            string storageFileName                           = null,
            bool bypassContainerCreationValidation           = false,
            ICloudBlobProvider cloudBlobProvider             = null,
            IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
            IAppendBlobBlockWriter appendBlobBlockWriter     = null,
            long?blobSizeLimitBytes                          = null,
            int?retainedBlobCountLimit                       = null)
        {
            this.textFormatter = textFormatter;

            if (string.IsNullOrEmpty(storageContainerName))
            {
                storageContainerName = "logs";
            }

            if (string.IsNullOrEmpty(storageFileName))
            {
                storageFileName = "log.txt";
            }

            this.cloudBlobClient      = cloudBlobClient;
            this.storageContainerName = storageContainerName;
            blobNameFactory           = new BlobNameFactory(storageFileName);
            this.bypassContainerCreationValidation = bypassContainerCreationValidation;
            this.cloudBlobProvider       = cloudBlobProvider ?? new DefaultCloudBlobProvider();
            this.appendBlobBlockPreparer = appendBlobBlockPreparer ?? new DefaultAppendBlobBlockPreparer();
            this.appendBlobBlockWriter   = appendBlobBlockWriter ?? new DefaultAppendBlobBlockWriter();
            this.blobSizeLimitBytes      = blobSizeLimitBytes;
            this.retainedBlobCountLimit  = retainedBlobCountLimit;
        }
示例#3
0
        /// <summary>
        /// Construct a sink that saves logs to the specified storage account.
        /// </summary>
        /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param>
        /// <param name="textFormatter"></param>
        /// <param name="storageFolderName">Folder name that log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="bypassFolderCreationValidation">Bypass the exception in case the folder creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="appendBlobBlockPreparer"></param>
        /// <param name="appendBlobBlockWriter"></param>
        public AzureBlobStorageSink(
            CloudStorageAccount storageAccount,
            ITextFormatter textFormatter,
            string storageFolderName                         = null,
            string storageFileName                           = null,
            bool bypassFolderCreationValidation              = false,
            ICloudBlobProvider cloudBlobProvider             = null,
            IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
            IAppendBlobBlockWriter appendBlobBlockWriter     = null)
        {
            this.textFormatter = textFormatter;

            if (string.IsNullOrEmpty(storageFolderName))
            {
                storageFolderName = "logs";
            }

            if (string.IsNullOrEmpty(storageFileName))
            {
                storageFileName = "log.txt";
            }

            this.storageAccount    = storageAccount;
            this.storageFolderName = storageFolderName;
            blobNameFactory        = new BlobNameFactory(storageFileName);
            this.bypassFolderCreationValidation = bypassFolderCreationValidation;
            this.cloudBlobProvider       = cloudBlobProvider ?? new DefaultCloudBlobProvider();
            this.appendBlobBlockPreparer = appendBlobBlockPreparer ?? new DefaultAppendBlobBlockPreparer();
            this.appendBlobBlockWriter   = appendBlobBlockWriter ?? new DefaultAppendBlobBlockWriter();
        }
示例#4
0
        /// <summary>
        /// Adds a sink that writes log events as records in Azure Blob Storage blob (default name LogEventEntity) using the given
        /// storage account name and Shared Access Signature (SAS) URL.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="formatter">Use a Serilog ITextFormatter such as CompactJsonFormatter to store object in data column of Azure blob</param>
        /// <param name="sharedAccessSignature">The storage account/blob SAS key.</param>
        /// <param name="accountName">The storage account name.</param>
        /// <param name="blobEndpoint">The (optional) blob endpoint. Only needed for testing.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            ITextFormatter formatter,
            string sharedAccessSignature,
            string accountName,
            Uri blobEndpoint = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName = null,
            bool writeInBatches    = false,
            TimeSpan?period        = null,
            int?batchPostingLimit  = null,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes    = null,
            int?retainedBlobCountLimit = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            if (string.IsNullOrWhiteSpace(accountName))
            {
                throw new ArgumentNullException(nameof(accountName));
            }
            if (string.IsNullOrWhiteSpace(sharedAccessSignature))
            {
                throw new ArgumentNullException(nameof(sharedAccessSignature));
            }

            try
            {
                //  TODO-VPL:  very likely messed it up here, not sure what that special case was supposed to do
                //  with a no-endpoint client
                if (blobEndpoint == null)
                {
                    throw new NotSupportedException($"'{nameof(blobEndpoint)}' must be provided");
                }
                else
                {
                    var credentials       = new AzureSasCredential(sharedAccessSignature);
                    var blobServiceClient = new BlobServiceClient(blobEndpoint, credentials);

                    // We set bypassBlobCreationValidation to true explicitly here as the the SAS URL might not have enough permissions to query if the blob exists.
                    return(AzureBlobStorage(loggerConfiguration, formatter, blobServiceClient, restrictedToMinimumLevel, storageContainerName, storageFileName, writeInBatches, period, batchPostingLimit, true, cloudBlobProvider, blobSizeLimitBytes, retainedBlobCountLimit));
                }
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Error configuring AzureBlobStorage: {ex}");

                ILogEventSink sink = new LoggerConfiguration().CreateLogger();
                return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
            }
        }
        /// <summary>
        /// Adds a sink that writes log events as records in Azure Blob Storage blob (default name LogEventEntity) using the given
        /// storage account name and Shared Access Signature (SAS) URL.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="formatter">Use a Serilog ITextFormatter such as CompactJsonFormatter to store object in data column of Azure blob</param>
        /// <param name="sharedAccessSignature">The storage account/blob SAS key.</param>
        /// <param name="accountName">The storage account name.</param>
        /// <param name="blobEndpoint">The (optional) blob endpoint. Only needed for testing.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            ITextFormatter formatter,
            string sharedAccessSignature,
            string accountName,
            Uri blobEndpoint = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName = null,
            bool writeInBatches    = false,
            TimeSpan?period        = null,
            int?batchPostingLimit  = null,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes    = null,
            int?retainedBlobCountLimit = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            if (string.IsNullOrWhiteSpace(accountName))
            {
                throw new ArgumentNullException(nameof(accountName));
            }
            if (string.IsNullOrWhiteSpace(sharedAccessSignature))
            {
                throw new ArgumentNullException(nameof(sharedAccessSignature));
            }

            try
            {
                var credentials = new StorageCredentials(sharedAccessSignature);
                CloudStorageAccount storageAccount;
                if (blobEndpoint == null)
                {
                    storageAccount = new CloudStorageAccount(credentials, accountName, endpointSuffix: null, useHttps: true);
                }
                else
                {
                    storageAccount = new CloudStorageAccount(credentials, blobEndpoint, null, null, null);
                }

                // We set bypassBlobCreationValidation to true explicitly here as the the SAS URL might not have enough permissions to query if the blob exists.
                return(AzureBlobStorage(loggerConfiguration, formatter, storageAccount, restrictedToMinimumLevel, storageContainerName, storageFileName, writeInBatches, period, batchPostingLimit, true, cloudBlobProvider, blobSizeLimitBytes, retainedBlobCountLimit));
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Error configuring AzureBlobStorage: {ex}");

                ILogEventSink sink = new LoggerConfiguration().CreateLogger();
                return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
            }
        }
示例#6
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account.
 /// </summary>
 /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="batchSizeLimit"></param>
 /// <param name="period"></param>
 /// <param name="storageFolderName">Folder name that log entries will be written to.</param>
 /// <param name="storageFileName">File name that log entries will be written to.</param>
 /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
 public AzureBatchingBlobStorageSink(
     CloudStorageAccount storageAccount,
     IFormatProvider formatProvider,
     ITextFormatter textFormatter,
     int batchSizeLimit,
     TimeSpan period,
     string storageFolderName             = null,
     string storageFileName               = null,
     ICloudBlobProvider cloudBlobProvider = null)
     : this(storageAccount, textFormatter, batchSizeLimit, period, storageFolderName, storageFileName, cloudBlobProvider : cloudBlobProvider)
 {
 }
示例#7
0
        /// <summary>
        /// Adds a sink that writes log events as records in an Azure Blob Storage blob (default LogEventEntity) using the given storage account.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="formatter">Use a Serilog ITextFormatter such as CompactJsonFormatter to store object in Azure blob</param>
        /// <param name="blobServiceClient">The Cloud Storage blob service client to use to insert the log entries to.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="bypassBlobCreationValidation">Bypass the exception in case the blob creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            ITextFormatter formatter,
            BlobServiceClient blobServiceClient,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName               = null,
            bool writeInBatches                  = false,
            TimeSpan?period                      = null,
            int?batchPostingLimit                = null,
            bool bypassBlobCreationValidation    = false,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes              = null,
            int?retainedBlobCountLimit           = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            if (blobServiceClient == null)
            {
                throw new ArgumentNullException(nameof(blobServiceClient));
            }
            if (blobSizeLimitBytes != null && blobSizeLimitBytes < 1)
            {
                throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
            }
            if (retainedBlobCountLimit != null && retainedBlobCountLimit < 1)
            {
                throw new ArgumentException("Invalid value provided; retained blob count limit must be at least 1 or null.");
            }

            ILogEventSink sink;

            try
            {
                sink = writeInBatches ?
                       (ILogEventSink) new AzureBatchingBlobStorageSink(blobServiceClient, formatter, batchPostingLimit ?? DefaultBatchPostingLimit, period ?? DefaultPeriod, storageContainerName, storageFileName, bypassBlobCreationValidation, cloudBlobProvider, blobSizeLimitBytes: blobSizeLimitBytes, retainedBlobCountLimit: retainedBlobCountLimit) :
                       new AzureBlobStorageSink(blobServiceClient, formatter, storageContainerName, storageFileName, bypassBlobCreationValidation, cloudBlobProvider, blobSizeLimitBytes: blobSizeLimitBytes, retainedBlobCountLimit: retainedBlobCountLimit);
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Error configuring AzureBlobStorage: {ex}");
                sink = new LoggerConfiguration().CreateLogger();
            }

            return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
        }
示例#8
0
        /// <summary>
        /// Adds a sink that writes log events as records in Azure Blob Storage blob (default name 'log.txt') using the given
        /// storage account name and Shared Access Signature (SAS) URL.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="sharedAccessSignature">The storage account/blob SAS key.</param>
        /// <param name="accountName">The storage account name.</param>
        /// <param name="blobEndpoint">The (optional) blob endpoint. Only needed for testing.</param>
        /// <param name="outputTemplate"> The template to use for writing log entries. The default is '[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}'</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            string sharedAccessSignature,
            string accountName,
            Uri blobEndpoint = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName               = null,
            string outputTemplate                = null,
            bool writeInBatches                  = false,
            TimeSpan?period                      = null,
            int?batchPostingLimit                = null,
            IFormatProvider formatProvider       = null,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes              = null,
            int?retainedBlobCountLimit           = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (string.IsNullOrWhiteSpace(accountName))
            {
                throw new ArgumentNullException(nameof(accountName));
            }
            if (string.IsNullOrWhiteSpace(sharedAccessSignature))
            {
                throw new ArgumentNullException(nameof(sharedAccessSignature));
            }

            if (string.IsNullOrEmpty(outputTemplate))
            {
                outputTemplate = DefaultConsoleOutputTemplate;
            }

            return(AzureBlobStorage(
                       loggerConfiguration,
                       new MessageTemplateTextFormatter(outputTemplate, formatProvider),
                       sharedAccessSignature,
                       accountName,
                       blobEndpoint,
                       restrictedToMinimumLevel,
                       storageContainerName,
                       storageFileName,
                       writeInBatches,
                       period,
                       batchPostingLimit,
                       cloudBlobProvider,
                       blobSizeLimitBytes,
                       retainedBlobCountLimit));
        }
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account.
 /// </summary>
 /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="textFormatter">The text formatter to use.</param>
 /// <param name="batchSizeLimit"></param>
 /// <param name="period"></param>
 /// <param name="storageContainerName">Container where the log entries will be written to.</param>
 /// <param name="storageFileName">File name that log entries will be written to.</param>
 /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
 /// <param name="appendBlobBlockPreparer"></param>
 /// <param name="appendBlobBlockWriter"></param>
 public AzureBatchingBlobStorageSink(
     CloudStorageAccount storageAccount,
     IFormatProvider formatProvider,
     ITextFormatter textFormatter,
     int batchSizeLimit,
     TimeSpan period,
     string storageContainerName                      = null,
     string storageFileName                           = null,
     ICloudBlobProvider cloudBlobProvider             = null,
     IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
     IAppendBlobBlockWriter appendBlobBlockWriter     = null)
     : this(storageAccount, textFormatter, batchSizeLimit, period, storageContainerName, storageFileName, cloudBlobProvider : cloudBlobProvider, appendBlobBlockPreparer : appendBlobBlockPreparer, appendBlobBlockWriter : appendBlobBlockWriter)
 {
 }
示例#10
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account.
 /// </summary>
 /// <param name="blobServiceClient">The Cloud Storage Client to use to insert the log entries to.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="textFormatter">The text formatter to use.</param>
 /// <param name="storageContainerName">Container where the log entries will be written to.</param>
 /// <param name="storageFileName">File name that log entries will be written to.</param>
 /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
 /// <param name="appendBlobBlockPreparer"></param>
 /// <param name="appendBlobBlockWriter"></param>
 /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
 /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
 /// <param name="useUtcTimeZone">Use UTC Timezone for logging events.</param>
 public AzureBatchingBlobStorageSink(
     BlobServiceClient blobServiceClient,
     IFormatProvider formatProvider,
     ITextFormatter textFormatter,
     string storageContainerName                      = null,
     string storageFileName                           = null,
     ICloudBlobProvider cloudBlobProvider             = null,
     IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
     IAppendBlobBlockWriter appendBlobBlockWriter     = null,
     long?blobSizeLimitBytes                          = null,
     int?retainedBlobCountLimit                       = null,
     bool useUtcTimeZone = false)
     : this(blobServiceClient, textFormatter, storageContainerName, storageFileName, cloudBlobProvider : cloudBlobProvider, appendBlobBlockPreparer : appendBlobBlockPreparer, appendBlobBlockWriter : appendBlobBlockWriter, blobSizeLimitBytes : blobSizeLimitBytes, retainedBlobCountLimit : retainedBlobCountLimit, useUtcTimeZone : useUtcTimeZone)
 {
 }
示例#11
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account.
 /// </summary>
 /// <param name="cloudBlobClient">The Cloud Storage Client to use to insert the log entries to.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="textFormatter">The text formatter to use.</param>
 /// <param name="batchSizeLimit"></param>
 /// <param name="period"></param>
 /// <param name="storageContainerName">Container where the log entries will be written to.</param>
 /// <param name="storageFileName">File name that log entries will be written to.</param>
 /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
 /// <param name="appendBlobBlockPreparer"></param>
 /// <param name="appendBlobBlockWriter"></param>
 /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
 /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
 public AzureBatchingBlobStorageSink(
     CloudBlobClient cloudBlobClient,
     IFormatProvider formatProvider,
     ITextFormatter textFormatter,
     int batchSizeLimit,
     TimeSpan period,
     string storageContainerName                      = null,
     string storageFileName                           = null,
     ICloudBlobProvider cloudBlobProvider             = null,
     IAppendBlobBlockPreparer appendBlobBlockPreparer = null,
     IAppendBlobBlockWriter appendBlobBlockWriter     = null,
     long?blobSizeLimitBytes                          = null,
     int?retainedBlobCountLimit                       = null)
     : this(cloudBlobClient, textFormatter, batchSizeLimit, period, storageContainerName, storageFileName, cloudBlobProvider : cloudBlobProvider, appendBlobBlockPreparer : appendBlobBlockPreparer, appendBlobBlockWriter : appendBlobBlockWriter, blobSizeLimitBytes : blobSizeLimitBytes, retainedBlobCountLimit : retainedBlobCountLimit)
 {
 }
示例#12
0
        /// <summary>
        /// Adds a sink that writes log events as records in Azure Blob Storage blob (default name 'log.txt') using the given
        /// storage account connection string.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="connectionString">The Cloud Storage Account connection string to use to insert the log entries to.</param>
        /// <param name="outputTemplate"> The template to use for writing log entries. The default is '[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}'</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="bypassBlobCreationValidation">Bypass the exception in case the blob creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            string connectionString,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName               = null,
            string outputTemplate                = null,
            bool writeInBatches                  = false,
            TimeSpan?period                      = null,
            int?batchPostingLimit                = null,
            bool bypassBlobCreationValidation    = false,
            IFormatProvider formatProvider       = null,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes              = null,
            int?retainedBlobCountLimit           = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            if (string.IsNullOrEmpty(outputTemplate))
            {
                outputTemplate = DefaultConsoleOutputTemplate;
            }

            return(AzureBlobStorage(
                       loggerConfiguration,
                       new MessageTemplateTextFormatter(outputTemplate, formatProvider),
                       connectionString,
                       restrictedToMinimumLevel,
                       storageContainerName,
                       storageFileName,
                       writeInBatches,
                       period,
                       batchPostingLimit,
                       bypassBlobCreationValidation,
                       cloudBlobProvider,
                       blobSizeLimitBytes,
                       retainedBlobCountLimit));
        }
示例#13
0
        /// <summary>
        /// Adds a sink that writes log events as records in an Azure Blob Storage blob (default LogEventEntity) using the given storage account.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="formatter">Use a Serilog ITextFormatter such as CompactJsonFormatter to store object in Azure blob</param>
        /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="storageFolderName">Folder name that log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="bypassBlobCreationValidation">Bypass the exception in case the blob creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            ITextFormatter formatter,
            CloudStorageAccount storageAccount,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageFolderName             = null,
            string storageFileName               = null,
            bool writeInBatches                  = false,
            TimeSpan?period                      = null,
            int?batchPostingLimit                = null,
            bool bypassBlobCreationValidation    = false,
            ICloudBlobProvider cloudBlobProvider = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            if (storageAccount == null)
            {
                throw new ArgumentNullException(nameof(storageAccount));
            }

            ILogEventSink sink;

            try
            {
                sink = writeInBatches ?
                       (ILogEventSink) new AzureBatchingBlobStorageSink(storageAccount, formatter, batchPostingLimit ?? DefaultBatchPostingLimit, period ?? DefaultPeriod, storageFolderName, storageFileName, bypassBlobCreationValidation, cloudBlobProvider) :
                       new AzureBlobStorageSink(storageAccount, formatter, storageFolderName, storageFileName, bypassBlobCreationValidation, cloudBlobProvider);
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Error configuring AzureBlobStorage: {ex}");
                sink = new LoggerConfiguration().CreateLogger();
            }

            return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
        }
示例#14
0
        /// <summary>
        /// Adds a sink that writes log events as records in Azure Blob Storage blob (default name 'log.txt') using the given
        /// storage account connection string.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="formatter">Use a Serilog ITextFormatter such as CompactJsonFormatter to store object in data column of Azure blob</param>
        /// <param name="connectionString">The Cloud Storage Account connection string to use to insert the log entries to.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="storageContainerName">Container where the log entries will be written to.</param>
        /// <param name="storageFileName">File name that log entries will be written to.</param>
        /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition
        /// key used for the events so is not enabled by default.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="bypassBlobCreationValidation">Bypass the exception in case the blob creation fails.</param>
        /// <param name="cloudBlobProvider">Cloud blob provider to get current log blob.</param>
        /// <param name="blobSizeLimitBytes">The maximum file size to allow before a new one is rolled, expressed in bytes.</param>
        /// <param name="retainedBlobCountLimit">The number of latest blobs to be retained in the container always. Deletes older blobs when this limit is crossed.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration AzureBlobStorage(
            this LoggerSinkConfiguration loggerConfiguration,
            ITextFormatter formatter,
            string connectionString,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string storageContainerName            = null,
            string storageFileName               = null,
            bool writeInBatches                  = false,
            TimeSpan?period                      = null,
            int?batchPostingLimit                = null,
            bool bypassBlobCreationValidation    = false,
            ICloudBlobProvider cloudBlobProvider = null,
            long?blobSizeLimitBytes              = null,
            int?retainedBlobCountLimit           = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            try
            {
                var blobServiceClient = new BlobServiceClient(connectionString);

                return(AzureBlobStorage(loggerConfiguration, formatter, blobServiceClient, restrictedToMinimumLevel, storageContainerName, storageFileName, writeInBatches, period, batchPostingLimit, bypassBlobCreationValidation, cloudBlobProvider, blobSizeLimitBytes, retainedBlobCountLimit));
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Error configuring AzureBlobStorage: {ex}");

                ILogEventSink sink = new LoggerConfiguration().CreateLogger();
                return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
            }
        }