Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettify the the diagnostic file content #5685

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ static Func<string, Task> BuildDefaultDiagnosticsWriter(HostingComponent.Configu
var startupDiagnosticsFileName = $"{configuration.EndpointName}-configuration.txt";
var startupDiagnosticsFilePath = Path.Combine(diagnosticsRootPath, startupDiagnosticsFileName);

return data => AsyncFile.WriteText(startupDiagnosticsFilePath, data);

return data =>
{
var prettied = JsonPrettyPrinter.Print(data);
return AsyncFile.WriteText(startupDiagnosticsFilePath, prettied);
};
}

static readonly ILog logger = LogManager.GetLogger<HostStartupDiagnosticsWriter>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace NServiceBus
{
using System.Text;

static class JsonPrettyPrinter
{
const string LINE_INDENT = " ";

internal static string Print(string input)
{
var builder = new StringBuilder(input.Length);
var quoted = false;
var indent = 0;

for (var i = 0; i < input.Length; i++)
{
var ch = input[i];
switch (ch)
{
case '{':
case '[':
builder.Append(ch);
if (!quoted) PrintIndent(builder, ++indent);
break;
case '}':
case ']':
if(!quoted) PrintIndent(builder, --indent);
builder.Append(ch);
break;
case '"':
builder.Append(ch);
var escaped = IsEscaped(input, i);
if (!escaped) quoted = !quoted;
break;
case ',':
builder.Append(ch);
if (!quoted) PrintIndent(builder, indent);
break;
case ':':
builder.Append(ch);
if (!quoted) builder.Append(" ");
break;
default:
builder.Append(ch);
break;
}
}

return builder.ToString();
}

static bool IsEscaped(string input, int i)
{
var escaped = false;
var index = i;
while (index > 0 && input[--index] == '\\')
{
escaped = !escaped;
}
return escaped;
}

static void PrintIndent(StringBuilder sb, int indent)
{
sb.AppendLine();
for (var i = 0; i <= indent; i++)
{
sb.Append(LINE_INDENT);
}
}
}
}