What is the simplest way to run a C# program in linux (eg with `dotnet`) and be able to see the output of Debug.Print statements? All the instructions I find on Google only show how to do this in VS Code and I am not currently using VS Code.
I have to say I already thought C#/.NET was the most search-engine-unfriendly programming language/environment in the world just based on those names, but then in 2015 when Microsoft decided to name a core component "dotnet" was when the unsearchability REALLY went into hyperdrive
Need to look up documentation on how i do a thing in dotnet. No not how I do it in .NET, how I do it in dotnet. If the documentation calls it .NET it's going to be giving me incorrect directions. Only dotnet is okay.

This turned out to be my "simplest" solution! https://wandering.shop/@xgranade/112961883517691345

It does turn out "Debug.Listeners" was renamed to "Trace.Listeners" in .NET CORE so the actual solution is just stuffing these two lines at the top of the program:

var myWriter = new TextWriterTraceListener(System.Console.Out);
Trace.Listeners.Add(myWriter);

Xandra Granade 🏳️‍⚧️ (@[email protected])

@[email protected] Does it work to add System.Console.Out as a trace listener? https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.listeners?view=netframework-4.8.1#examples (I have no idea why the docs keep redirecting to .NET Framework 4.8.1 instead of .NET neé .NET Core.)

The Wandering Shop
@mcc we never really forgave Google for grabbing "go", which is an English stopword
@mcc anyway, we never made this work. sorry :/
@ireneista @mcc They stole it from an older less known programming language too. 🤬
@dalias @mcc so they did. but of course it suddenly had a ton of search juice once it was theirs, which is definitely not the result of anti-competitive practices (sarcasm).

@mcc
I used DuckDuckGo to search:
dotnet core debug.print -"VS code" -"Visual Studio"

The third link was: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-stack

Which led me to:
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/tools-overview?source=recommendations

Which suggests VSCode... but also mentions:
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-sos

Which supposedly supports lldb?

I then used DDG to search:
dotnet core sos debug print

Which led to this blog post:
https://devblogs.microsoft.com/premier-developer/debugging-net-core-with-sos-everywhere/

Good luck!

dotnet-stack diagnostic tool - .NET CLI - .NET

Learn how to install and use the dotnet-stack CLI tool which captures and prints the managed stacks for all threads in the target .NET process.

@djm Thank you very much, this is helpful
@mcc Microsoft's secret plan to defeat Google right here. Become unsearchable! :D
@mcc 👀 I've just been using mono and it's working pretty well for me! Though I'm not sure if I'm seeing debug statements
@mcc non-committal answer: I think you can `dotnet build path/to/dir/containing/the/sln/file` the thing and just run it? Fuck, I don't know what sort of control the output log levels or whatever.
@onelson Debug.Print isn't a "log level". It sends information to an attached debugger, and if no debugger is attached it is a noop
@mcc what the fuck, geez
@onelson Microsoft ¯\_(ツ)_/¯
@mcc I would simply give up
@onelson I mean seriously the answer is probably going to be "just fuck off, and ask Christine to run it tomorrow on her Windows machine, where this stuff is set up"
@mcc I mean, I think VS Code is something you can get pretty trivially. I don't know what sort of baggage there is to do dotnet stuff in there. If it were Visual Studio (non-Code) I'm assuming it's all greased lightning.

@onelson I have vs code on this machine but there's an awkward issue where we've already got separate, unique vs code execution scripts set up for Windows and Mac hosts and this raises the question of (1) will the Mac launch.json simply work on linux? (2) will the Mac launch.json need modification to work on Linux? (3) will the Mac launch.json do something *subtly but silently wrong*, such as misinvoke msbuild prebuild steps, on Linux?

Simple to test but simpler to ask Christine to test it!

@mcc Yeah, I agree. Christine is the fix.

@mcc @onelson What does your *.csproj look like?

is OutputType "Exe" or "WinExe"?

I just use System.Console.WriteLine in a pinch, but that only works for "WinExe" as I understand it...

I'd test more but pretty sure you're already getting feedback from Christine at this point.

@mcc @onelson not quite a noop, it goes to syslog
https://github.com/dotnet/runtime/blob/b1968e7aa8d56a088e8be6817d5240fb345f901c/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Unix.cs#L48

e.g.
`Debug.WriteLine("this is some output " + DateTime.Now.ToString(), "foo");`

`$ tail -F /var/log/syslog`
...
`Aug 14 14:38:29 sandsys dotnet_hell: foo: this is some output 2024-08-14 2:38:29 p.m.`

runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.Unix.cs at b1968e7aa8d56a088e8be6817d5240fb345f901c · dotnet/runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime

GitHub
@mcc @onelson (with just a regular `dotnet run` of a `dotnet new console` project with nothing else special)
@rakslice @onelson D: this might actually be worse than doing nothing
@mcc @onelson absolutely, but it 100% fits with the Microsoft mentality about this sort of stuff
@rakslice @mcc you can send the logs to dmesg by editing a key in the linux registry

@onelson @mcc "editing a key in the linux registry" alol.

but to be serious, it's syslog output so a variety of syslog implementations are available and most of those you can configure to deliver different categories of output a variety of different ways to different targets (e.g. different files, devices, etc.) or not

@mcc I think you can type `dotnet new` to bootstrap a project. And then `dotnet run` to run it.

It’s been a while since I’ve done C# on Linux.

@mcc sorry, after reading your question more carefully, I don’t think what I said was helpful. Good luck.

@mcc Does it work to add System.Console.Out as a trace listener?

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.listeners?view=netframework-4.8.1#examples

(I have no idea why the docs keep redirecting to .NET Framework 4.8.1 instead of .NET neé .NET Core.)

Debug.Listeners Property (System.Diagnostics)

Gets the collection of listeners that is monitoring the debug output.

@xgranade @mcc

lol

The main thing I work on is still using .Net Framework 4.8 - because we just don't have the staff or time to sink a big effort into upgrading - so of course any time *I* try to look anything up, Microsoft directs me to the documentation for the latest .NET aka Core.

MS must be doing some very sophisticated context tracking to so consistently send users to the wrong docs version.

@mcc you may need to use something like netcoredbg, which is somehow even less convenient than gdb but shouldn't be too difficult to use for something simple like this
@mrcarrot Thanks, and you can confirm this works with .Net Core/"dotnet", as opposed to older systems like mono?
@mcc it does. As far as I know, it's the only FOSS tool that does this(and as such the only one that bothers to even slightly work outside of a specific IDE).

@mcc Before you call Debug.Print, try adding these lines:

Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;

(I'm using .NET 8)

@mcc Super simple examples:

This just outputs "Hello, World!":

private static void Main(string[] args)
{
Debug.Print("This is a debug message.");
Console.WriteLine("Hello, World!");
}

This outputs "This is a debug message." and "Hello, World!":

private static void Main(string[] args)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;

Debug.Print("This is a debug message.");
Console.WriteLine("Hello, World!");
}

@mcc

Microsoft Learn does is the source of all the good docs - https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build

TL;DR; The '-tl:auto' parameter is the answer - tl == 'terminal logging'

dotnet build command - .NET CLI

The dotnet build command builds a project and all of its dependencies.