I tried many ways, but I could not manage to write logs in the console of my .NET Framework 4.6 Console Application. I searched on Stackoverflow and found 2 possible solutions, which did not work:
- Registering in ConfigureDI as
services.AddLogging(configure => configure.AddConsole());
This first possible solution I could not even test as apparently
ILoggingBuilder does not contain a definition for AddConsole [...]
- Registering in ConfigureDI as
services.AddSingleton< ILoggerFactory, LoggerFactory>(); services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
I'm using ILogger with dependency injection, like this:
public class MyApplication
{
private readonly ILogger<MyService> _logger;
public MyApplication(ILogger<MyService> logger)
{
_logger = logger;
}
public void Run()
{
_logger.LogInformation("Application Started at {dateTime}", DateTime.UtcNow);
//...
}
}
My Program.cs is like this:
public static class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
DependencyInjectionConfiguration.ConfigureDI(services);
var serviceProvider = services.BuildServiceProvider();
var receiver = serviceProvider.GetService<IReceiver>();
receiver.MyServiceMethod();
}
}
and my ConfigureDI method is like this:
public static class DependencyInjectionConfiguration
{
public static void ConfigureDI(IServiceCollection services)
{
services.AddScoped<IReceiver, Receiver>();
services.AddHttpClient<MyClient>();
services.AddScoped<HelperXml>();
//services.AddSingleton<ILoggerFactory, LoggerFactory>();
//services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
//services.AddLogging(configure => configure.AddConsole());
}
}
Anyone has a clue on what is wrong here? In addition: Writing on a txt file would be nice too...
Thanks