protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            TableBatchOperation operation = new TableBatchOperation();

            string lastPartitionKey = null;

            foreach (var logEvent in events)
            {
                var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent);

                if (partitionKey != lastPartitionKey)
                {
                    lastPartitionKey = partitionKey;
                    if (operation.Count > 0)
                    {
                        await _table.ExecuteBatchAsync(operation);

                        operation = new TableBatchOperation();
                    }
                }
                var logEventEntity = new LogEventEntity(
                    logEvent,
                    _formatProvider,
                    partitionKey,
                    _keyGenerator.GenerateRowKey(logEvent)
                    );
                operation.Add(TableOperation.Insert(logEventEntity));
            }
            if (operation.Count > 0)
            {
                await _table.ExecuteBatchAsync(operation);
            }
        }
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            var table = _cloudTableProvider.GetCloudTable(_storageAccount, _storageTableName, _bypassTableCreationValidation);
            TableBatchOperation operation = new TableBatchOperation();

            string lastPartitionKey = null;

            foreach (var logEvent in events)
            {
                var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent);

                if (partitionKey != lastPartitionKey)
                {
                    lastPartitionKey = partitionKey;
                    if (operation.Count > 0)
                    {
                        await table.ExecuteBatchAsync(operation).ConfigureAwait(false);

                        operation = new TableBatchOperation();
                    }
                }
                var logEventEntity = new LogEventEntity(
                    logEvent,
                    _formatProvider,
                    partitionKey,
                    _keyGenerator.GenerateRowKey(logEvent)
                    );
                operation.Add(TableOperation.Insert(logEventEntity));
            }
            if (operation.Count > 0)
            {
                await table.ExecuteBatchAsync(operation).ConfigureAwait(false);
            }
        }
        /// <summary>
        ///     Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            var logEventEntity = new LogEventEntity(logEvent, _formatProvider,
                                                    _keyGenerator.GeneratePartitionKey(logEvent),
                                                    _keyGenerator.GenerateRowKey(logEvent)
                                                    );

            _table.ExecuteAsync(TableOperation.Insert(logEventEntity)).SyncContextSafeWait(_waitTimeoutMilliseconds);
        }
        /// <summary>
        /// Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            var table          = _cloudTableProvider.GetCloudTable(_storageAccount, _storageTableName, _bypassTableCreationValidation);
            var logEventEntity = new LogEventEntity(
                logEvent,
                _textFormatter,
                _keyGenerator.GeneratePartitionKey(logEvent),
                _keyGenerator.GenerateRowKey(logEvent)
                );

            table.ExecuteAsync(TableOperation.InsertOrMerge(logEventEntity))
            .SyncContextSafeWait(_waitTimeoutMilliseconds);
        }
예제 #5
0
        /// <summary>
        /// Creates a DynamicTableEntity for Azure Storage, given a Serilog <see cref="LogEvent"/>.Properties
        /// are stored as separate columns.
        /// </summary>
        /// <param name="logEvent">The event to log</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="additionalRowKeyPostfix">Additional postfix string that will be appended to row keys</param>
        /// <param name="keyGenerator">The IKeyGenerator for the PartitionKey and RowKey</param>
        /// <param name="propertyColumns">Specific properties to be written to columns. By default, all properties will be written to columns.</param>
        /// <returns></returns>
        public static DynamicTableEntity CreateEntityWithProperties(LogEvent logEvent, IFormatProvider formatProvider, string additionalRowKeyPostfix, IKeyGenerator keyGenerator, string[] propertyColumns = null)
        {
            var tableEntity = new DynamicTableEntity
            {
                PartitionKey = keyGenerator.GeneratePartitionKey(logEvent),
                RowKey       = keyGenerator.GenerateRowKey(logEvent, additionalRowKeyPostfix),
                Timestamp    = logEvent.Timestamp
            };

            var dynamicProperties = tableEntity.Properties;

            dynamicProperties.Add("MessageTemplate", new EntityProperty(logEvent.MessageTemplate.Text));
            dynamicProperties.Add("Level", new EntityProperty(logEvent.Level.ToString()));
            dynamicProperties.Add("RenderedMessage", new EntityProperty(logEvent.RenderMessage(formatProvider)));

            if (logEvent.Exception != null)
            {
                dynamicProperties.Add("Exception", new EntityProperty(logEvent.Exception.ToString()));
            }

            List <KeyValuePair <ScalarValue, LogEventPropertyValue> > additionalData = null;
            var  count = dynamicProperties.Count;
            bool isValid;

            foreach (var logProperty in logEvent.Properties)
            {
                isValid = IsValidColumnName(logProperty.Key) && ShouldIncludeProperty(logProperty.Key, propertyColumns);

                // Don't add table properties for numeric property names
                if (isValid && (count++ < _maxNumberOfPropertiesPerRow - 1))
                {
                    dynamicProperties.Add(logProperty.Key, AzurePropertyFormatter.ToEntityProperty(logProperty.Value, null, formatProvider));
                }
                else
                {
                    if (additionalData == null)
                    {
                        additionalData = new List <KeyValuePair <ScalarValue, LogEventPropertyValue> >();
                    }
                    additionalData.Add(new KeyValuePair <ScalarValue, LogEventPropertyValue>(new ScalarValue(logProperty.Key), logProperty.Value));
                }
            }

            if (additionalData != null)
            {
                dynamicProperties.Add("AggregatedProperties", AzurePropertyFormatter.ToEntityProperty(new DictionaryValue(additionalData), null, formatProvider));
            }

            return(tableEntity);
        }