GitHub user eventhorizon-cli edited a discussion: csharp: replace nlog with 
Microsoft.Extensions.Logging

The logs of csharp client are written to files by default, and cannot be 
written only to the console, this is not very friendly to the .net developers.

1. This kind of logging is not in line with the normal practice of .NET 
developers. 
2. If it is a product environment, long-term log file squeezes can cause 
certain issues.
3. The control of log output is not flexible enough.

The more commonly used logging system in .net sdk is  
Microsoft.Extensions.Logging.
https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line

The sdk can expose an API for configuring LoggerFactory, allowing users to 
configure it freely.

The design of EF Core can be referenced.
https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/extensions-logging?tabs=v3#other-application-types

I tried to modify the logger in rocketmq client. Here is what it looks like 
after modification.

```C#
public static class MqLogManager
{
    private static ILoggerFactory _loggerFactory;

    public static ILogger<T> CreateLogger<T>()
    {
        return _loggerFactory?.CreateLogger<T>() ?? NullLogger<T>.Instance;
    }
    
    public static ILogger CreateLogger(string categoryName)
    {
        return _loggerFactory?.CreateLogger(categoryName) ?? 
NullLogger.Instance;
    }

    public static void UseLoggerFactory(ILoggerFactory loggerFactory)
    {
        _loggerFactory = loggerFactory ?? throw new 
ArgumentNullException(nameof(loggerFactory));
    }
}
```

usages:

```C#
MqLogManager.UseLoggerFactory(LoggerFactory.Create(builder =>
{
    builder.AddConsole();
}));

public abstract class Client
{
    private static readonly ILogger Logger = 
MqLogManager.CreateLogger<Client>();

    protected virtual async Task Start()
    {
        Logger.LogDebug($"Begin to start the rocketmq client, 
clientId={ClientId}");
    }
}
```

If possible, I would like to submit a PR, thanks.

GitHub link: https://github.com/apache/rocketmq-clients/discussions/593

----
This is an automatically sent email for dev@rocketmq.apache.org.
To unsubscribe, please send an email to: dev-unsubscr...@rocketmq.apache.org

Reply via email to