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); } }
private HttpContent GetHttpContent(LogEntryDto dto) { try { return(CreateHttpContent(dto)); } catch (Exception e) { dto = ProcessLoggingException(e); return(CreateHttpContent(dto)); } }
private void WriteLogEntry(LogEntryDto entryDto) { string json = JsonConvert.SerializeObject(entryDto, Formatting.Indented); string level = entryDto.Level.ToLower(); if (level == "error" || level == "fatal") { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(json); Console.ResetColor(); }
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); }