Daddoon / BlazorMobile

Create full C# driven hybrid-apps for iOS, Android, UWP & Desktop with Blazor!
MIT License
413 stars 55 forks source link

Console.WriteLine() not working #167

Closed fabianschurz closed 4 years ago

fabianschurz commented 4 years ago

Hi, when debugging the BlazorMobile project with console any Console.WriteLine calls are not printed in the console.

I don't know if this is by design, at the moment i'm using jsinterop with console.log to print debug stuff inside the web browsers console instead of the console running aspnetcore.

fabianschurz commented 4 years ago

It's working when running the Blazor Server project so probably this is not possible in the client project i guess. Am I right?

Daddoon commented 4 years ago

On wich platform are you testing your Console.WriteLine, and where were you expecting the output ? (Native or Web ?).

You may encounter some difference between platforms because of their way of working, and what is running under the hood.

Generally speaking:

Thats why i'm using this little helper present in the BlazorMobile library (but not exposed):

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

[assembly: InternalsVisibleTo("BlazorMobile.UWP")]
[assembly: InternalsVisibleTo("BlazorMobile.Android")]
[assembly: InternalsVisibleTo("BlazorMobile.iOS")]
[assembly: InternalsVisibleTo("BlazorMobile")]
namespace BlazorMobile.Common.Helpers
{
    internal static class ConsoleHelper
    {
        public static void WriteLine(string message)
        {
            message = $"INFO: {message}";

            switch (BlazorDevice.RuntimePlatform)
            {
                case BlazorDevice.UWP:
                    Debug.WriteLine(message);
                    break;
                default:
                    Console.WriteLine(message);
                    break;
            }
        }

        public static void WriteError(string message)
        {
            message = $"ERROR: {message}";

            switch (BlazorDevice.RuntimePlatform)
            {
                case BlazorDevice.UWP:
                    Debug.WriteLine(message);
                    break;
                default:
                    Console.WriteLine(message);
                    break;
            }
        }

        public static void WriteException(Exception ex)
        {
            if (ex == null)
            {
                return;
            }

            WriteError(ex.Message);
        }
    }
}

Generally speaking, if you have any logs issue or ambiguity, i suggest you to redirect your messages to the native side (or web if you prefer...depend of what you are more used to) with an interop service, while you are debugging.

fabianschurz commented 4 years ago

Thank you. This answered my question and fixes the issue.