ILogger question.

How does ILogger's LogInformation() work? I thought it was to output to a console, debug output or anywhere I wanted? I had to supply a Debug.WriteLine() just to see something.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using TitanStudio.Core.Interfaces;

namespace TitanStudio.Core.ViewModel;
public partial class MainViewModel : ObservableObject, IMainViewModel
{
private readonly ILogger<MainViewModel> _logger;

// Only need this if you use private methods!!!
// Example: void IMainViewModel.Test() => Test();

public MainViewModel(ILogger<MainViewModel> logger)
{
_logger = logger;
}

[RelayCommand]
public void Test()
{
_logger.LogInformation("TEST");
Debug.WriteLine(":)");
}
}
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using TitanStudio.Core.Interfaces;

namespace TitanStudio.Core.ViewModel;
public partial class MainViewModel : ObservableObject, IMainViewModel
{
private readonly ILogger<MainViewModel> _logger;

// Only need this if you use private methods!!!
// Example: void IMainViewModel.Test() => Test();

public MainViewModel(ILogger<MainViewModel> logger)
{
_logger = logger;
}

[RelayCommand]
public void Test()
{
_logger.LogInformation("TEST");
Debug.WriteLine(":)");
}
}
Thank you.
11 Replies
Sehra
Sehra•4w ago
are you following https://medium.com/@shalahuddinshanto/dependency-injection-in-wpf-a-complete-implementation-guide-468abcf95337 ? add package Microsoft.Extensions.Logging.Debug (or .Console if you want it there), then on the AddLogging(), chain .AddDebug() or .AddConsole()
Temporal Nightmare
Temporal NightmareOP•4w ago
Ohh ok. What if I wanted to output to a ListBox?
Sehra
Sehra•4w ago
think you'd have to write your own ILoggerProvider for that
Temporal Nightmare
Temporal NightmareOP•4w ago
ah, that I'll suck with probablyh but i can use console for now, thank you Also, I put the ILogger on my .Core project and not Wpf, does it matter?
Temporal Nightmare
Temporal NightmareOP•4w ago
No description
Sehra
Sehra•4w ago
your ILogger? it's provided by the package Microsoft.Extensions.Logging (actually .Abstractions but it's transitive)
Temporal Nightmare
Temporal NightmareOP•4w ago
Ah okay, removed then.
Sehra
Sehra•4w ago
for the core, you can add package Microsoft.Extensions.Logging.Abstractions if all you want is ILogger
Temporal Nightmare
Temporal NightmareOP•4w ago
Yep, I got that installed too. I'm trying to figure out how to load a View via this DI thing, confused. besides the main view of course I already registered everything
Sehra
Sehra•4w ago
i don't really do any wpf so ui people might know
Temporal Nightmare
Temporal NightmareOP•4w ago
It's okay, I think I'm close to solving it. Thank you for your help though 🙂

Did you find this page helpful?