I have a WPF project which I would like to build and run from the command line instead of Visual Studio. This can be done with the dotnet run command from the project folder, but no debug logs are output to the console.
As suggested by ChatGPT, I am using this code in App.xaml.cs:
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace YourNamespace
{
public partial class App : Application
{
public static IServiceProvider ServiceProvider { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
var logger = ServiceProvider.GetRequiredService<ILogger<App>>();
logger.LogInformation("Application started in Debug mode.");
}
private void ConfigureServices(IServiceCollection services)
{
services.AddLogging(configure =>
{
configure.ClearProviders(); // Clear default providers
configure.AddConsole(); // Add console logging
}).Configure<LoggerFilterOptions>(options =>
{
options.MinLevel = LogLevel.Debug; // Set minimum log level to Debug
});
}
}
}
I have all necessary NuGet packages installed, and the program runs. There is just no logging output whatsoever.