示例#1
0
        internal static string JsonSerializePayload(EventEntry entry)
        {
            try
            {
                using (var writer = new StringWriter(CultureInfo.InvariantCulture))
                    using (var jsonWriter = new JsonTextWriter(writer)
                    {
                        Formatting = Newtonsoft.Json.Formatting.Indented, CloseOutput = false
                    })
                    {
                        EventEntryUtil.JsonWritePayload(jsonWriter, entry);
                        jsonWriter.Flush();
                        return(writer.ToString());
                    }
            }
            catch (JsonWriterException jwe)
            {
                SemanticLoggingEventSource.Log.EventEntrySerializePayloadFailed(jwe.ToString());

                var errorDictionary = new Dictionary <string, object>
                {
                    {
                        "Error",
                        string.Format(CultureInfo.CurrentCulture, Properties.Resources.JsonSerializationError, jwe.Message)
                    }
                };

                return(JsonConvert.SerializeObject(errorDictionary, Newtonsoft.Json.Formatting.Indented));
            }
        }
        /// <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);
        }