Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
Custom Json Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKotyza committed Sep 4, 2020
1 parent fc20613 commit 279a3df
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
10 changes: 5 additions & 5 deletions serilog-sinks-couchdb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "..\ConsoleApp1\ConsoleApp1.csproj", "{3A0E8F51-8939-4617-A402-C0D54F2F6826}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "..\ConsoleApp1\ConsoleApp1.csproj", "{4BAC2D42-D719-4740-912A-F6BD08C7D891}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -22,10 +22,10 @@ Global
{E728D183-07C4-49F7-8C4C-D76D70C8F3B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E728D183-07C4-49F7-8C4C-D76D70C8F3B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E728D183-07C4-49F7-8C4C-D76D70C8F3B4}.Release|Any CPU.Build.0 = Release|Any CPU
{3A0E8F51-8939-4617-A402-C0D54F2F6826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A0E8F51-8939-4617-A402-C0D54F2F6826}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A0E8F51-8939-4617-A402-C0D54F2F6826}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A0E8F51-8939-4617-A402-C0D54F2F6826}.Release|Any CPU.Build.0 = Release|Any CPU
{4BAC2D42-D719-4740-912A-F6BD08C7D891}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BAC2D42-D719-4740-912A-F6BD08C7D891}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BAC2D42-D719-4740-912A-F6BD08C7D891}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BAC2D42-D719-4740-912A-F6BD08C7D891}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions src/Serilog.Sinks.CouchDB/Serilog.Sinks.CouchDB.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<description>Serilog event sink that writes to a CouchDB database over HTTP.</description>
<language>en-US</language>
<projectUrl>http://serilog.net</projectUrl>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
<iconUrl>http://serilog.net/images/serilog-sink-nuget.png</iconUrl>
<license type="expression">Apache-2.0</license>


<tags>serilog logging couchdb</tags>
<dependencies>
<dependency id="Serilog" version="[1.5.1,2)" />
Expand Down
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.CouchDB/Sinks/CouchDB/CouchDBJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo

// Better not to allocate an array in the 99.9% of cases where this is false
// ReSharper disable once PossibleMultipleEnumeration

output.Write(",\"RenderedMessage\":\"");
foreach (var r in logEvent.MessageTemplate.Tokens)
{
var space = new StringWriter();
r.Render(logEvent.Properties, space);
output.Write(space.ToString().Replace("\"",""));
}
output.Write("\"");


if (tokensWithFormat.Any())
{
output.Write(",\"@r\":[");
Expand Down
49 changes: 4 additions & 45 deletions src/Serilog.Sinks.CouchDB/Sinks/CouchDB/CouchDBSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,57 +90,16 @@ public CouchDBSink(string databaseUrl, int batchPostingLimit, TimeSpan period, I
public async void Emit(LogEvent logEvent)
{

var payload2 = new StringWriter();

payload2.Write("{\"docs\":[");
JsonValueFormatter jsonValueFormatter = new JsonValueFormatter();
CoachDBJsonFormatter formatter2 = new CoachDBJsonFormatter(jsonValueFormatter);


var payload = new StringWriter();

payload.Write("{\"docs\":[");



JsonFormatter formatter = new JsonFormatter(
omitEnclosingObject: true,
formatProvider: _formatProvider,
renderMessage: true);

var delimStart = "{";

payload.Write(delimStart);

JsonValueFormatter jsonValueFormatter = new JsonValueFormatter();
CoachDBJsonFormatter formatter = new CoachDBJsonFormatter(jsonValueFormatter);

formatter.Format(logEvent, payload);
payload.Write(
",\"UtcTimestamp\":\"{0:u}\"}}",
logEvent.Timestamp.ToUniversalTime().DateTime);
delimStart = ",{";

payload.Write("]}");

// payload.Write(delimStart);


/*
formatter.Format(logEvent, payload);
payload.Write(
",\"UtcTimestamp\":\"{0:u}\"}",
logEvent.Timestamp.ToUniversalTime().DateTime);
delimStart = ",{";
*/
/*
//{{"docs":[{"Timestamp":"2020-09-02T17:04:25.4863907+02:00","Level":"Error","MessageTemplate":"Hello {World} {array}!","RenderedMessage":"Hello \"World\" \"0102\"!","Properties":{"World":"World","array":"0102"},"UtcTimestamp":"2020-09-02 15:04:25Z"}}
*/


formatter2.Format(logEvent, payload2);
payload2.Write("]}");


var content = new StringContent(payload2.ToString(), Encoding.UTF8, "application/json");
var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
var result = await _httpClient.PostAsync(BulkUploadResource, content);
if (!result.IsSuccessStatusCode)
throw new LoggingFailedException(string.Format("Received failed result {0} when posting events to CouchDB", result.StatusCode));
Expand Down

0 comments on commit 279a3df

Please sign in to comment.