2

When my Blazor WASM project was using .net core project 3.1, Debug.WriteLine("test") would output to Chromes console window.

After upgrading everything to .net core 5.0.301 Nothing is output.

I get this error show in the Visual Studio to debug console.

fail: Microsoft.WebAssembly.Diagnostics.DevToolsProxy[0]
      sending error response for id: msg-62E669E5DBE5C53DAF0973BE80FD50E4:::1032 -> [Result: IsOk: False, IsErr: True, Value: , Error: {
        "result": {
          "type": "object",
          "subtype": "error",
          "description": "Cannot find member named 'Debug'.",
          "className": "ReferenceError"
        }
      } ]

Does anyone have any ideas?

ca53rlb
  • 77
  • 5
  • 2
    No, but you can use Console.WriteLine as a workaround ... – Alamakanambra Jun 09 '21 at 19:09
  • Thanks for the suggestion of using Console.WriteLine. The problem with using console.writeline is it's not omitted from the compiled code when deployed in release mode. Where as Debug.WriteLine is flagged with [Conditional("DEBUG")] which causes it to completely be removed from the compiled code. My problem is something has changed in the framework or Visual Studio updates which has caused Debug.WriteLine to stop working in my Blazor WAS, app. Also, with less code compiled, equates to smaller DLL's to download. – ca53rlb Jun 10 '21 at 21:47
  • This looks like a .net 5 problem, I created a basic solution in .net 5 debug.writeline does not work, whereas a basic solution in .net 3.1 does work – ca53rlb Jun 14 '21 at 12:58
  • Interesting, can you share the exact code (gh repo), I will try. – Alamakanambra Jun 15 '21 at 19:14
  • 2
    I raised the question on Microsoft answers they confirmed it's an issue and raised the following GitHub ticket https://github.com/dotnet/runtime/issues/54213#issuecomment-861602236 If you want to try it for yourself, Create a new Blazor wasm project in .net 5.0 add two lines in the IncrementCount method Debug.WriteLine("from debug"); Console.WriteLine("from console"); You will see only console it written. If you create the same new project but this time using the .net 3.1 framework and the same two lines of code you will see both Debug & Console messages. – ca53rlb Jun 16 '21 at 20:07

1 Answers1

2

Although said bug is claimed to be fixed in .NET 6, in .NET 7 I (still or yet again, I did not test it) can't use Debug.WriteLine() to output text to the browser console: The texts just never show up.

In order to achieve the desired effect - that is, debugging to the browser console in Debug mode, while in release mode these debugging texts shall be omitted -, I do it like this:

[Conditional("DEBUG")]
public static void ConsoleDebug(string text)
{
    Console.WriteLine(text);
}

This way, when I do something like ConsoleDebug("something went wrong");, it's being printed to the browser console when run in debug mode, whereas when in release mode, nothing happens.

If you make a component (e.g. DebugToConsole.razor) containing the code above, you can simply put @using static DebugToConsole in _Imports.razor and use it in all your components via ConsoleDebug("your text").

Philipp Koch
  • 69
  • 10
  • As I'm revisiting this, I've come to think that maybe (depending on what one actually wants to achieve) it might also be as simple as just do [logging in Blazor the way it's supposed to be done according to Microsoft](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/logging). – Philipp Koch Aug 20 '23 at 22:32