1

我在Net Core中写了这个日志代码,有人提到“这可以封装在扩展方法中”。这是一种更简单的方法吗,最好在 Net Core 2.2 中编写?

启动.cs

services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddLogging(builder => builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace));
4

4 回答 4

0

虽然这不可能是一个纯粹的 stackoverflow 问题,但我想我可以提供一个意见:

该方法的问题ConfigureServices是您需要定义所有依赖项,对于较大的系统,这可能意味着痛苦的冗长配置方法,维护起来非常困难和无聊。

要解决此问题,您可以通过划分所有配置来简化启动,为此,您可以使用 C# 的扩展方法,该方法允许“扩展”类(添加更多方法)而无需修改源代码班级。

扩展方法很简单,取一个带有静态方法的静态类:

public static class CommonServicesExtension
{

    public static IServiceCollection AddCommonServices(IServiceCollection services)
    {
        services.AddHttpClient();
        services.AddNodaDateTime();
        services.AddMemoryCache();
        services.AddCurrencyExchange();
        // Add and configure many things

        return services;
    }
}

现在你可以像这样使用它:

CommonServicesExtension.AddCommonServices(serviceColletion);

但这有点丑陋且难以阅读,因此,只需添加this到您的扩展方法中:

public static IServiceCollection AddCommonServices(this IServiceCollection services)

通过此更改,您可以像 net core 一样调用您的配置!

services.AddCommonServices();

这样,在您的配置服务中,您的配置方法从

public IServiceCollection ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();

    services.AddDbContext<CoreContext>(options => options.UseMySql(Configuration.GetConnectionString("Core")));
    services.AddDbContext<CoreReadOnlyContext>(options => options.UseMySql(Configuration.GetConnectionString("CORE_READONLY")));
    services.AddDbContext<StatsContext>(options =>
        options.UseMySql(Configuration.GetConnectionString("Stats")));


    services.AddScoped<ProfileTokenGenerator>();
    services.AddTransient<MailNotificationService>();
    services.AddTransient<RegisterControllerService>();

    services.AddScoped<OtherClass>();
    services.AddTransient<MoreTransient>();
    services.AddTransient<ThingsService>();

    return services;
}

public IServiceCollection ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddContexts();
    services.AddCommonServices();
    services.AddOtherServices();
    return services;
}

如您所见,更干净,更易于阅读!

于 2019-07-30T18:15:42.327 回答
0

创建一个静态文件,例如;

public static class StartupConfiguration
{
    public static void AddMyLogging(
        this IServiceCollection services)
    {
        services.AddSingleton<ILoggerFactory, LoggerFactory>();
        services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
        services.AddLogging(builder => builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace));
    }
}

然后在您的 Startup 课程中:

    public void ConfigureServices(
        IServiceCollection services)
    {
        services.AddMyLogging();
    }
于 2019-07-30T18:16:06.373 回答
-1

好吧,它可以按照AddLogging扩展方法的编写方式来完成。

public static IServiceCollection AddCustomLogging (this IServiceCollection services) {  
    services.AddSingleton<ILoggerFactory, LoggerFactory>();
    services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
    services.AddLogging(builder => builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace)); 
}
于 2019-07-30T18:07:39.413 回答
-1

只调用AddLogging扩展方法就足够了。

services.AddLogging(builder => builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace)); 

因为,如果您查看此扩展方法的实现,您可以看到ILoggerFactory并且ILogger<>已经应该将其添加到服务中。

services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof (ILogger<>), typeof (Logger<>)));
于 2019-07-30T18:20:13.580 回答