Beispiel #1
0
        /// <summary>
        /// Write the formatted event output.
        /// </summary>
        /// <param name="eventEntry">The event data to be formatted.</param>
        /// <param name="writer">The writer to receive the formatted output.</param>
        public void WriteEvent(EventEntry eventEntry, TextWriter writer)
        {
            Guard.ArgumentNotNull(eventEntry, "eventEntry");

            using (var jsonWriter = new JsonTextWriter(writer)
            {
                CloseOutput = false, Formatting = this.formatting
            })
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName(PropertyNames.ProviderId);
                jsonWriter.WriteValue(eventEntry.ProviderId);
                jsonWriter.WritePropertyName(PropertyNames.EventId);
                jsonWriter.WriteValue(eventEntry.EventId);
                jsonWriter.WritePropertyName(PropertyNames.Keywords);
                jsonWriter.WriteValue((long)eventEntry.Schema.Keywords);
                jsonWriter.WritePropertyName(PropertyNames.Level);
                jsonWriter.WriteValue((int)eventEntry.Schema.Level);
                jsonWriter.WritePropertyName(PropertyNames.Message);
                jsonWriter.WriteValue(eventEntry.FormattedMessage);
                jsonWriter.WritePropertyName(PropertyNames.Opcode);
                jsonWriter.WriteValue((int)eventEntry.Schema.Opcode);
                jsonWriter.WritePropertyName(PropertyNames.Task);
                jsonWriter.WriteValue((int)eventEntry.Schema.Task);
                jsonWriter.WritePropertyName(PropertyNames.Version);
                jsonWriter.WriteValue(eventEntry.Schema.Version);
                jsonWriter.WritePropertyName(PropertyNames.Payload);
                EventEntryUtil.JsonWritePayload(jsonWriter, eventEntry);
                jsonWriter.WritePropertyName(PropertyNames.EventName);
                jsonWriter.WriteValue(eventEntry.Schema.EventName);
                jsonWriter.WritePropertyName(PropertyNames.Timestamp);
                jsonWriter.WriteValue(eventEntry.GetFormattedTimestamp(this.DateTimeFormat));

                if (eventEntry.ActivityId != Guid.Empty)
                {
                    jsonWriter.WritePropertyName(PropertyNames.ActivityId);
                    jsonWriter.WriteValue(eventEntry.ActivityId);
                }

                if (eventEntry.RelatedActivityId != Guid.Empty)
                {
                    jsonWriter.WritePropertyName(PropertyNames.RelatedActivityId);
                    jsonWriter.WriteValue(eventEntry.RelatedActivityId);
                }

                jsonWriter.WriteEndObject();

                // Write an entry separator so all the logs can be read as an array,
                // adding the [] chars to the raw written data ( i.e: "[" + raw + "]" )
                // where raw = {log1},{log2}, ... {logN},
                jsonWriter.WriteRaw(EntrySeparator);

                // Writes new line when indented
                if (jsonWriter.Formatting == Newtonsoft.Json.Formatting.Indented)
                {
                    jsonWriter.WriteRaw("\r\n");
                }
            }
        }
        /// <summary>
        /// Converts an <see cref="EventEntry"/> to a <see cref="EventRecord"/>.
        /// </summary>
        /// <param name="entry">The entry to convert.</param>
        /// <returns>A converted entry, or <see langword="null"/> if the payload is invalid.</returns>
        public static EventRecord TryConvertToEventRecord(this EventEntry entry)
        {
            var entity = new EventRecord()
            {
                ProviderId       = entry.ProviderId,
                ProviderName     = entry.Schema.ProviderName,
                EventId          = entry.EventId,
                EventKeywords    = (long)entry.Schema.Keywords,
                Level            = (int)entry.Schema.Level,
                Opcode           = (int)entry.Schema.Opcode,
                Task             = (int)entry.Schema.Task,
                Timestamp        = entry.Timestamp,
                Version          = entry.Schema.Version,
                FormattedMessage = entry.FormattedMessage,
                Payload          = EventEntryUtil.JsonSerializePayload(entry)
            };

            return(entity);
        }
Beispiel #3
0
        private static DataTable GetDataTable(string instanceName, IEnumerable <EventEntry> collection)
        {
            var table = new DataTable();

            table.Columns.Add("InstanceName", typeof(string));
            table.Columns.Add("ProviderId", typeof(Guid));
            table.Columns.Add("ProviderName", typeof(string));
            table.Columns.Add("EventId", typeof(int));
            table.Columns.Add("EventKeywords", typeof(long));
            table.Columns.Add("Level", typeof(string));
            table.Columns.Add("Opcode", typeof(int));
            table.Columns.Add("Task", typeof(int));
            table.Columns.Add("Timestamp", typeof(DateTime));
            table.Columns.Add("Version", typeof(int));
            table.Columns.Add("FormattedMessage", typeof(string));
            table.Columns.Add("Payload", typeof(string));

            foreach (var entry in collection)
            {
                var values = new object[]
                {
                    instanceName,
                    entry.Schema.ProviderId,
                    entry.Schema.ProviderName,
                    (int)entry.EventId,
                    (long)entry.Schema.Keywords,
                    (int)entry.Schema.Level,
                    (int)entry.Schema.Opcode,
                    (int)entry.Schema.Task,
                    entry.Timestamp.UtcDateTime,
                    (int)entry.Schema.Version,
                    (object)entry.FormattedMessage ?? DBNull.Value,
                    (object)EventEntryUtil.JsonSerializePayload(entry) ?? DBNull.Value
                };

                table.Rows.Add(values);
            }

            return(table);
        }
Beispiel #4
0
        internal static SqlDataRecord ToSqlDataRecord(this EventEntry record, string instanceName)
        {
            var sqlDataRecord = new SqlDataRecord(SqlMetaData);

            sqlDataRecord.SetValue(0, instanceName ?? string.Empty);
            sqlDataRecord.SetValue(1, record.ProviderId);
            sqlDataRecord.SetValue(2, record.Schema.ProviderName ?? string.Empty);
            sqlDataRecord.SetValue(3, record.EventId);
            sqlDataRecord.SetValue(4, (long)record.Schema.Keywords);
            sqlDataRecord.SetValue(5, (int)record.Schema.Level);
            sqlDataRecord.SetValue(6, (int)record.Schema.Opcode);
            sqlDataRecord.SetValue(7, (int)record.Schema.Task);
            sqlDataRecord.SetValue(8, record.Timestamp);
            sqlDataRecord.SetValue(9, record.Schema.Version);
            sqlDataRecord.SetValue(10, (object)record.FormattedMessage ?? DBNull.Value);
            sqlDataRecord.SetValue(11, (object)EventEntryUtil.JsonSerializePayload(record) ?? DBNull.Value);
            sqlDataRecord.SetValue(12, record.ActivityId);
            sqlDataRecord.SetValue(13, record.RelatedActivityId);
            sqlDataRecord.SetValue(14, record.ProcessId);
            sqlDataRecord.SetValue(15, record.ThreadId);

            return(sqlDataRecord);
        }
        public static DynamicTableEntity CreateTableEntity(this CloudEventEntry entry)
        {
            var dictionary = new Dictionary <string, EntityProperty>();

            dictionary.Add("EventId", new EntityProperty(entry.EventId));
            dictionary.Add("EventDate", new EntityProperty(entry.EventDate));
            dictionary.Add("Keywords", new EntityProperty(entry.Keywords));
            dictionary.Add("ProviderId", new EntityProperty(entry.ProviderId));
            dictionary.Add("ProviderName", new EntityProperty(entry.ProviderName));
            dictionary.Add("InstanceName", new EntityProperty(entry.InstanceName));
            dictionary.Add("Level", new EntityProperty(entry.Level));
            if (entry.Message != null)
            {
                dictionary.Add("Message", new EntityProperty(Normalize(entry.Message)));
            }

            dictionary.Add("Opcode", new EntityProperty(entry.Opcode));
            dictionary.Add("Task", new EntityProperty(entry.Task));
            dictionary.Add("Version", new EntityProperty(entry.Version));

            // Create a "Payload"
            if (entry.Payload != null && entry.Payload.Count > 0)
            {
                var json = EventEntryUtil.JsonSerializePayload(entry.Payload);
                if (json.Length > MaxStringLength)
                {
                    dictionary.Add("Payload", new EntityProperty("{ 'payload_serialization_error':'The payload is too big to serialize.' }"));
                }
                else
                {
                    dictionary.Add("Payload", new EntityProperty(json));

                    foreach (var item in entry.Payload.Take(MaxPayloadItems))
                    {
                        var value = item.Value;
                        if (value != null)
                        {
                            EntityProperty property = null;
                            var            type     = value.GetType();

                            if (type == typeof(string))
                            {
                                property = new EntityProperty((string)value);
                            }
                            else if (type == typeof(int))
                            {
                                property = new EntityProperty((int)value);
                            }
                            else if (type == typeof(long))
                            {
                                property = new EntityProperty((long)value);
                            }
                            else if (type == typeof(double))
                            {
                                property = new EntityProperty((double)value);
                            }
                            else if (type == typeof(Guid))
                            {
                                property = new EntityProperty((Guid)value);
                            }
                            else if (type == typeof(bool))
                            {
                                property = new EntityProperty((bool)value);
                            }
                            else if (type.IsEnum)
                            {
                                var typeCode = ((Enum)value).GetTypeCode();
                                if (typeCode <= TypeCode.Int32)
                                {
                                    property = new EntityProperty(Convert.ToInt32(value, CultureInfo.InvariantCulture));
                                }
                                else
                                {
                                    property = new EntityProperty(Convert.ToInt64(value, CultureInfo.InvariantCulture));
                                }
                            }
                            else if (type == typeof(byte[]))
                            {
                                property = new EntityProperty((byte[])value);
                            }

                            //// TODO: add & review DateTimeOffset if it's supported

                            if (property != null)
                            {
                                dictionary.Add(string.Format(CultureInfo.InvariantCulture, "Payload_{0}", item.Key), property);
                            }
                        }
                    }
                }
            }

            return(new DynamicTableEntity(entry.PartitionKey, entry.RowKey, null, dictionary));
        }