Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUI : LogMessage are not showing up anywhere #7821

Closed
nssidhu opened this issue Jun 7, 2022 · 9 comments · Fixed by #8180
Closed

MAUI : LogMessage are not showing up anywhere #7821

nssidhu opened this issue Jun 7, 2022 · 9 comments · Fixed by #8180
Assignees
Labels

Comments

@nssidhu
Copy link

nssidhu commented Jun 7, 2022

Description

I am using Inject ILogger and writing log message, but i don't see any output anywhere.

public ILogger _Logger { get; set; }

 protected override Task OnInitializedAsync()
{
    _Logger.LogInformation("Log Message from ILogger");
    System.Diagnostics.Debug.WriteLine("Log Message from Diganostic");
    Console.WriteLine("Log Message from console");
}

Is there something else to be configured in Blazor MAUI ?

Steps to Reproduce

  1. Create Blazor MAUI App
  2. In Index Page, after injecting the ILogger, inside OnInitializedAsync event
protected override Task OnInitializedAsync()
{
    _Logger.LogInformation("Log Message from ILogger");
    System.Diagnostics.Debug.WriteLine("Log Message from Diganostic");
    Console.WriteLine("Log Message from console");
}

Version with bug

6.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

Android emulator

Did you find any workaround?

No

Relevant log output

None
@nssidhu nssidhu added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Jun 7, 2022
@Eilon Eilon added the area-blazor Blazor Hybrid / Desktop, BlazorWebView label Jun 7, 2022
@javiercn
Copy link
Member

javiercn commented Jun 7, 2022

@nssidhu thanks for contacting us.

Are you configuring a logging provider? Unlike asp.net core or blazor webassembly, I don't believe .NET Maui apps register a default ILoggerProvider implementation.

@nssidhu
Copy link
Author

nssidhu commented Jun 7, 2022

@nssidhu thanks for contacting us.

Are you configuring a logging provider? Unlike asp.net core or blazor webassembly, I don't believe .NET Maui apps register a default ILoggerProvider implementation.

No I am not doing anything extra when compared to Blazor Wasm.
Any links to documents which tell about how to configure it ?

@mkArtakMSFT mkArtakMSFT added area-core-hosting Extensions / Hosting / AppBuilder / Startup and removed area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Jun 7, 2022
@mkArtakMSFT mkArtakMSFT changed the title Blazor MAUI : LogMessage are not showing up anywhere MAUI : LogMessage are not showing up anywhere Jun 7, 2022
@jfversluis jfversluis removed the s/needs-verification Indicates that this issue needs initial verification before further triage will happen label Jun 9, 2022
@Eilon
Copy link
Member

Eilon commented Jun 13, 2022

I just started taking a look at this and here's what I've found so far. I've tested a default MAUI / MAUI Blazor app by selectively adding certain loggers (or none at all) in MauiProgram.cs like so:

services.AddLogging(logging =>
{
    logging.AddDebug();
    //logging.AddConsole();
    //logging.AddSimpleConsole();
    //logging.AddSimpleConsole(o => o.ColorBehavior = LoggerColorBehavior.Disabled);
});
Logger Windows Android Sim on Win iOS Sim MacCatalyst
Default (None) None None None None
Debug VS Output
NS.ClassName: Information: TheLogMessage
VS Output
[0:] NS.ClassName: Information: TheLogMessage
VS Mac Application Output
NS.ClassName: Information: TheLogMessage
VS Mac Application Output
NS.ClassName: Information: TheLogMessage
Console None VS Output
[DOTNET] �[40m�[32minfo�[39m�[22m�[49m: NS.ClassName[0]
[DOTNET] TheLogMessage
VS Mac Application Output
2022-06-14 20:49:10.853949-0700 MauiLogTest[31558:1500095] \^[[40m\^[[32minfo\^[[39m\^[[22m\^[[49m:
NS.ClassName[0]
TheLogMessage
VS Mac Application Output
2022-06-14 16:28:29.176 MauiLogTest[30459:1442744] �[40m�[32minfo�[39m�[22m�[49m:
NS.ClassName[0]
TheLogMessage
SimpleConsole None VS Output
[DOTNET] �[40m�[32minfo�[39m�[22m�[49m: NS.ClassName[0]
[DOTNET] TheLogMessage
VS Mac Application Output
2022-06-14 20:52:28.331803-0700 MauiLogTest[31637:1504813] \^[[40m\^[[32minfo\^[[39m\^[[22m\^[[49m:
NS.ClassName[0]
TheLogMessage
VS Mac Application Output
2022-06-14 20:32:28.651 MauiLogTest[30926:1475771] �[40m�[32minfo�[39m�[22m�[49m:
NS.ClassName[0]
TheLogMessage
SimpleConsole
(Color=Disabled)
None VS Output
[DOTNET] info: NS.ClassName[0]
[DOTNET] TheLogMessage
VS Mac Application Output
2022-06-14 20:54:20.232128-0700 MauiLogTest[31700:1508058] info: NS.ClassName[0]
TheLogMessage
VS Mac Application Output
2022-06-14 20:36:54.491 MauiLogTest[30983:1479987] info: NS.ClassName[0]
TheLogMessage

Notes:

  • By default, MAUI apps have no default log sinks at all:
    private void ConfigureDefaultLogging()
    {
    // By default, if no one else has configured logging, add a "no-op" LoggerFactory
    // and Logger services with no providers. This way when components try to get an
    // ILogger<> from the IServiceProvider, they don't get 'null'.
    Services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, NullLoggerFactory>());
    Services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(NullLogger<>)));
    }
  • For Android, iOS, and MacCatalyst, Console/SimpleConsole appear to be emitting ANSI color codes, which causes the output to be hard to read, because the VS output window and VS for Mac App Output window don't support ANSI color codes.
  • I believe we should eventually come up with a set of loggers+options that work "out of the box" for all MAUI configurations, and enable them in the template, wrapped in #if DEBUG ... logging.AddMauiLogging() ... #endif, so that it has no default cost in MAUI itself, and is trivial for developers to tweak, if needed.
  • Android Sim on Mac was not tested (not working on my machine 😢)
  • Physical Android and iOS devices were not tested

@nssidhu
Copy link
Author

nssidhu commented Jun 13, 2022

@Eilon

Thanks for looking at this.

I really Like the Blazor Hybrid approach over XAML Native, this is opening up the doors for Web Developers to write Native Mobile Apps as well ability to write once and run everywhere.

!! Great work and kudos to entire team !!

Few question

I had to use builder prefix to service, which is different than what you have provided above.

 builder.Services.AddLogging(logging =>
        {
            logging.AddDebug();
            //logging.AddConsole();
            //logging.AddSimpleConsole();
            //logging.AddSimpleConsole(o => o.ColorBehavior = LoggerColorBehavior.Disabled);
        });

Do I need to install any nuget package to get this working ?

Severity Code Description Project File Line Suppression State
Error CS1061 'ILoggingBuilder' does not contain a definition for 'AddDebug' and no accessible extension method 'AddDebug' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?)

@Eilon
Copy link
Member

Eilon commented Jun 14, 2022

@nssidhu for the Debug logger, add a reference to this package: https://www.nuget.org/packages/Microsoft.Extensions.Logging.Debug

And you're right, in my case I had an extra variable to shorten the syntax. By default in MauiProgram.cs you would need to do builder.Services....

@Eilon
Copy link
Member

Eilon commented Jun 15, 2022

The grid above is now filled out.

@Eilon

This comment was marked as outdated.

@Eilon
Copy link
Member

Eilon commented Jun 17, 2022

Not sure what I was thinking earlier, but for some reason I thought we needed to conditionally add debug vs. console loggers. I completely forgot that the Debug logger just works everywhere!

So my proposal is quite simple:

The various .NET MAUI project templates need two changes:

  1. Add a reference in the CSPROJ to <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
  2. Add these lines of code to MauiProgram.cs:
    #if DEBUG
    		builder.Logging.AddDebug();
    #endif

And that will enable a good default logging experience on every platform.

The code of the Microsoft.Extensions.Logging.Debug reference should be trivial because it brings in no real new dependencies (it depends on other things that are already referenced in all .NET MAUI projects), and the amount of code in it is trivial (a few dozen lines of mostly uninteresting code): https://github.com/dotnet/runtime/tree/main/src/libraries/Microsoft.Extensions.Logging.Debug/src

@Eilon
Copy link
Member

Eilon commented Jun 20, 2022

PR to call AddDebug() in the project template: #8180

@Eilon Eilon added this to the .NET 7 milestone Jun 20, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants