This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transfer multiline log messages from Node to .NET without treating ea…
…ch line as a separate log entry
- Loading branch information
1 parent
f4efcac
commit fae0a88
Showing
6 changed files
with
161 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Microsoft.AspNetCore.NodeServices/TypeScript/HttpNodeInstanceEntryPoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Microsoft.AspNetCore.NodeServices/TypeScript/SocketNodeInstanceEntryPoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Microsoft.AspNetCore.NodeServices/TypeScript/Util/OverrideStdOutputs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// When Node writes to stdout/strerr, we capture that and convert the lines into calls on the | ||
// active .NET ILogger. But by default, stdout/stderr don't have any way of distinguishing | ||
// linebreaks inside log messages from the linebreaks that delimit separate log messages, | ||
// so multiline strings will end up being written to the ILogger as multiple independent | ||
// log messages. This makes them very hard to make sense of, especially when they represent | ||
// something like stack traces. | ||
// | ||
// To fix this, we intercept stdout/stderr writes, and replace internal linebreaks with a | ||
// marker token. When .NET receives the lines, it converts the marker tokens back to regular | ||
// linebreaks within the logged messages. | ||
// | ||
// Note that it's better to do the interception at the stdout/stderr level, rather than at | ||
// the console.log/console.error (etc.) level, because this takes place after any native | ||
// message formatting has taken place (e.g., inserting values for % placeholders). | ||
const findInternalNewlinesRegex = /\n(?!$)/g; | ||
const encodedNewline = '__ns_newline__'; | ||
|
||
encodeNewlinesWrittenToStream(process.stdout); | ||
encodeNewlinesWrittenToStream(process.stderr); | ||
|
||
function encodeNewlinesWrittenToStream(outputStream: NodeJS.WritableStream) { | ||
const origWriteFunction = outputStream.write; | ||
outputStream.write = <any>function (value: any) { | ||
// Only interfere with the write if it's definitely a string | ||
if (typeof value === 'string') { | ||
const argsClone = Array.prototype.slice.call(arguments, 0); | ||
argsClone[0] = encodeNewlinesInString(value); | ||
origWriteFunction.apply(this, argsClone); | ||
} else { | ||
origWriteFunction.apply(this, arguments); | ||
} | ||
}; | ||
} | ||
|
||
function encodeNewlinesInString(str: string): string { | ||
return str.replace(findInternalNewlinesRegex, encodedNewline); | ||
} |