예제 #1
0
        /// <summary>
        /// Writes an array of logging events to the log target. By default it iterates on all
        /// events and passes them to "Write" method. Inheriting classes can use this method to
        /// optimize batch writes.
        /// </summary>
        /// <param name="logEvents">Logging events to be written out.</param>
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            //must sort into containers and then into the blobs for the container
            if (_getTableNameDelegate == null)
            {
                _getTableNameDelegate = c => TableName.Render(c.LogEvent);
            }

            var tableBuckets = SortHelpers.BucketSort(logEvents, _getTableNameDelegate);

            //Iterate over all the tables being written to
            foreach (var tableBucket in tableBuckets)
            {
                var tableNameFinal = CheckAndRepairTableNamingRules(tableBucket.Key);
                InitializeTable(tableNameFinal);
                var batch = new TableBatchOperation();
                //add each message for the destination table limit batch to 100 elements
                foreach (var asyncLogEventInfo in tableBucket.Value)
                {
                    var entity = new NLogEntity(asyncLogEventInfo.LogEvent, Layout);
                    batch.Insert(entity);
                    if (batch.Count == 100)
                    {
                        _table.ExecuteBatch(batch);
                        batch.Clear();
                    }
                }
                if (batch.Count > 0)
                {
                    _table.ExecuteBatch(batch);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Writes an array of logging events to the log target. By default it iterates on all
        /// events and passes them to "Write" method. Inheriting classes can use this method to
        /// optimize batch writes.
        /// </summary>
        /// <param name="logEvents">Logging events to be written out.</param>
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            //must sort into containers and then into the blobs for the container
            if (_getContainerNameDelegate == null)
            {
                _getContainerNameDelegate = c => Container.Render(c.LogEvent);
            }

            var containerBuckets = SortHelpers.BucketSort(logEvents, _getContainerNameDelegate);

            //Iterate over all the containers being written to
            foreach (var containerBucket in containerBuckets)
            {
                var containerName = CheckAndRepairContainerNamingRules(containerBucket.Key);
                InitializeContainer(containerName);

                if (_getBlobNameDelegate == null)
                {
                    _getBlobNameDelegate = c => BlobName.Render(c.LogEvent);
                }

                var blobBuckets = SortHelpers.BucketSort(containerBucket.Value, _getBlobNameDelegate);

                //Iterate over all the blobs in the container to be written to
                foreach (var blobBucket in blobBuckets)
                {
                    var blobName = CheckAndRepairBlobNamingRules(blobBucket.Key);

                    InitializeBlob(blobName);

                    //Initilize StringBuilder size based on number of items to write. Default StringBuilder initialization size is 16 characters.
                    var logMessage = new StringBuilder(blobBucket.Value.Count * 128);

                    //add each message for the destination append blob
                    foreach (var asyncLogEventInfo in blobBucket.Value)
                    {
                        logMessage.AppendLine(Layout.Render(asyncLogEventInfo.LogEvent));
                    }

                    _appendBlob.AppendText(logMessage.ToString());
                }
            }
        }