コード例 #1
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);
        }
コード例 #2
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>
        /// <returns></returns>
        public static DynamicTableEntity CreateEntityWithProperties(LogEvent logEvent, IFormatProvider formatProvider, string additionalRowKeyPostfix)
        {
            var tableEntity = new DynamicTableEntity();

            tableEntity.PartitionKey = GenerateValidPartitionKey(logEvent);
            tableEntity.RowKey       = GenerateValidRowKey(logEvent, additionalRowKeyPostfix);
            tableEntity.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;
            int count = dynamicProperties.Count;

            foreach (var logProperty in logEvent.Properties)
            {
                if (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);
        }