'Writing logs in console in a .NET Framework 4.6 Console Application
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
Solution 1:[1]
You'll need to target the .NET Standard framework if you wish to use ILogger (part of Microsoft.Extensions.Logging).
Solution 2:[2]
I think it completely depends on the business considerations.
Sometimes we upload multiple posts with the same content on Instagram so we should keep one of them by deleting others. If there is a case that users are not allowed to create multiple data with the same content then they should get an error of duplication (409 status code). this is my all-time solution. maybe I did not get your point of view about that problem. so after reading your comments and a long while of searching around your issue finally I would recommend using an uuid like mechanism to prevent duplicate data due to any network failure before receiving success status from the app side. here is a procedure for this solution.
1. Create a unique UUID in your app.
2. Send the ID from step 1 with your post.
3. Store this ID inside the post that the user tried to send it also store it in your database.
4. If the user did not get any response from step 2 after a while.
4-a. On the next try send the UUID to check the status
4-b. If the status is not success go to step 2.
5. show the user that the post was submitted without any error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jboeke |
| Solution 2 |
