Beispiel #1
0
        private string GetLogEntryJson(LogEntryDto logEntry, bool wrapSerializationException = true)
        {
            try
            {
                string json = JsonConvert.SerializeObject(logEntry, JsonSerializerSettings);

                //customize the "data" property and use the document type as a property key instead.
                //this prevents indexing errors if two completely payloads have matching keys,
                //or if the data actually is a scalar value already.
                if (logEntry.Payload != null)
                {
                    string propertyName = String.IsNullOrEmpty(logEntry.PayloadType) ? logEntry.Context : logEntry.PayloadType;
                    propertyName = propertyName.Replace(".", "_");
                    propertyName = SnakeCasing.GetPropertyName(propertyName, false);
                    json         = json.Replace(LogEntryDto.PayloadPropertyPlaceholderName, propertyName);
                }

                return(json);
            }
            catch (Exception e)
            {
                //if something went wrong, return the exception as a serialized log entry
                if (wrapSerializationException)
                {
                    var dto = ProcessLoggingException(e);
                    return(GetLogEntryJson(dto, false));
                }

                //we couldn't even serialize the exception DTO. Can't happen, but make sure we
                //don't end up recursing indefinitely in case we introduce a severe bug
                return(null);
            }
        }
Beispiel #2
0
        private HttpContent CreateHttpContent(LogEntryDto content)
        {
            string json = JsonConvert.SerializeObject(content, JsonSerializerSettings);

            //customize the "data" property and use the document type as a property key instead.
            //this prevents indexing errors if two completely payloads have matching keys,
            //or if the data actually is a scalar value already.
            if (content.Payload != null)
            {
                string propertyName = String.IsNullOrEmpty(content.PayloadType) ? content.Context : content.PayloadType;
                propertyName = propertyName.Replace(".", "_");
                propertyName = SnakeCasing.GetPropertyName(propertyName, false);
                json         = json.Replace(LogEntryDto.PayloadPropertyPlaceholderName, propertyName);
            }

            var httpContent = new StringContent(json);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(httpContent);
        }