You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apart from beeing a convoluted mess and using way too much coloring using escape-sequences (using silkfire/Pastel), and that in this case I put out much more messages than normally for a debug session, my logger still uses only one Console.WriteLine() to output a message.
And still, the linebreak get's swallowed sometimes randomly on concurrent access.
How can that be? I thought it's thread-safe.
Configuration
Which version of .NET is the code running on? 5.0.300
What OS and version, and what distro if applicable? Windows 10 2004
What is the architecture (x64, x86, ARM, ARM64)? x64
Regression?
I use this logger since Core 3.0 and never noticed this, but I also never really fired so many messages concurrently maybe.
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
Not to comment on the thread safety issue, but as a workaround have you tried using Console.Write and append the linefeed yourself? It sounds like you're already assembling a string so maybe you have an opportunity to append the newline before the string is constructed.
Not to comment on the thread safety issue, but as a workaround have you tried using Console.Write and append the linefeed yourself? It sounds like you're already assembling a string so maybe you have an opportunity to append the newline before the string is constructed.
That is so simple, it didn't even come to my mind lol.
Is there even a difference beetween Console.Write() with a linebreak in the string and Console.WriteLine without?
Curious how this can happen. Looking at console implementation, Console uses TextWriter.Synchronized which has [MethodImpl(MethodImplOptions.Synchronized)] on every method.
If there is synchronization issue for new line, you should observe duplicate new line as well as missed new line. Your repro indicates that some new lines are eaten.
Replacing WriteLine(logString) with Write(logString & vbCrLf) fixed it for me, soo~
I don't bother to show my spagetthi code, but the project is private and I don't have time to extract it to something working right now, sorry.
And in this case called only from one method. In the case of the screenshot from this For Each:
ForEachremoteFileInremoteFilesLog(LogLvl.Debug3, $"{If(remoteFile.IsDirectory, "Dir", "File"),4} [{remoteFile.IsRegularFile}] {remoteFile.LastWriteTime} {remoteFile.FullName.Substring(remoteRelFilePathPos)}")'Ignore directorysIfremoteFile.IsDirectoryThenLog(LogLvl.Debug,remoteFile.Name&" is directory")ContinueForEndIf'Ignore system filesIfNotremoteFile.IsRegularFileThenLog(LogLvl.Quit,"RemoteFile is no regular file: "&remoteFile.Name)ContinueForEndIf'Get relative pathDimremoteRelFilePath=remoteFile.FullName.Substring(remoteRelFilePathPos)'Save to dictremoteFilesChecked.Add(remoteRelFilePath,remoteFile)'Add if file is not in local filesIfNotlocalFilesChecked.ContainsKey(remoteRelFilePath)ThenLog(LogLvl.Warning,"Local does not contain "&remoteFile.Name)toDownload.Add(remoteFile)ContinueForEndIf'Add if file is at least a second newer (to prevent precision errors with different file-systems)DimlocalLastWrite=localFilesChecked(remoteRelFilePath).LastWriteTimeUtc.AddSeconds(1)IflocalLastWrite.CompareTo(remoteFile.LastWriteTimeUtc)<0ThenLog(LogLvl.Info, $"{remoteFile.Name} is newer than local file (local < remote)"&vbCrLf& $"({localFilesChecked(remoteRelFilePath).LastWriteTimeUtc} < {remoteFile.LastWriteTimeUtc}) UTC"&vbCrLf& $"({localFilesChecked(remoteRelFilePath).LastWriteTime} < {remoteFile.LastWriteTime}) LOCAL")toDownload.Add(remoteFile)ContinueForEndIfLog(LogLvl.Success,remoteFile.Name&" is already up to date locally")Next
@remcoros, can you share a repro? The whole of WriteLine is inside of a lock (a MethodImplOptions.Synchronized method), so it's not clear how this could happen.
Actually, while trying to repro this. I think the bug is in System.Commandline. They provide an IConsole and wrap some input/output text readers and it looks like they are not thread safe.
Looking at .NET source I can't see how this could happen also. I think the issue of op might be the same as mine (bug in some console wrapper or the log library itself)
I'll make an issue on the System.Commandline repo.
Activity
MarkPflug commentedon Jun 5, 2021
Not to comment on the thread safety issue, but as a workaround have you tried using Console.Write and append the linefeed yourself? It sounds like you're already assembling a string so maybe you have an opportunity to append the newline before the string is constructed.
Fleischhauer commentedon Jun 5, 2021
That is so simple, it didn't even come to my mind lol.
Is there even a difference beetween Console.Write() with a linebreak in the string and Console.WriteLine without?
stephentoub commentedon Jun 5, 2021
Can you share a repro?
huoyaoyuan commentedon Jun 5, 2021
Curious how this can happen. Looking at console implementation, Console uses
TextWriter.Synchronizedwhich has[MethodImpl(MethodImplOptions.Synchronized)]on every method.If there is synchronization issue for new line, you should observe duplicate new line as well as missed new line. Your repro indicates that some new lines are eaten.
Fleischhauer commentedon Jun 5, 2021
Replacing WriteLine(logString) with Write(logString & vbCrLf) fixed it for me, soo~
I don't bother to show my spagetthi code, but the project is private and I don't have time to extract it to something working right now, sorry.
But I can show you the parts which cause it if that helps.
(Edit: Github seems to hate VB.Net too lol - https://pastebin.com/LbnFprk4)
The string was build like this:
And in this case called only from one method. In the case of the screenshot from this For Each:
remcoros commentedon Jan 19, 2022
I just ran into this too.
A console tool (net6.0) that runs multiple operations in parallel using Task.Run(..) and write progress using Console.WriteLine.
The result (when it happens) is the same as op, something like:
Using Console.Write(message + Environment.NewLine) also fixed it for me.
stephentoub commentedon Jan 19, 2022
@remcoros, can you share a repro? The whole of WriteLine is inside of a lock (a MethodImplOptions.Synchronized method), so it's not clear how this could happen.
remcoros commentedon Jan 19, 2022
Actually, while trying to repro this. I think the bug is in System.Commandline. They provide an IConsole and wrap some input/output text readers and it looks like they are not thread safe.
Looking at .NET source I can't see how this could happen also. I think the issue of op might be the same as mine (bug in some console wrapper or the log library itself)
I'll make an issue on the System.Commandline repo.
stephentoub commentedon Jan 19, 2022
That makes a lot more sense. Thanks!
terrajobst commentedon Apr 21, 2022
This could be closed then, right?
adamsitnik commentedon Nov 13, 2023
Right. Thank you all!