ABP .net Core 将日志打印在控制台

上效果图


来看一下操作流程:

一、分为.net Core 2.2 和 .net Core 3.0及以上

(一)、.net Core 2.2
1、在 EntityFrameworkCore中安装Nuget报: Microsoft.Extensions.Logging.Console
2、在XXXXEntityFrameworkModule增加
public static readonly LoggerFactory MyLoggerFactory
            = new LoggerFactory(new[]
        {
            new ConsoleLoggerProvider((category, level)
                => category == DbLoggerCategory.Database.Command.Name
                   && level == LogLevel.Information, true)
        });
Configuration.Modules.AbpEfCore().AddDbContext<YTMyprocteDbContext>(options =>
{
      if (options.ExistingConnection != null)
      {
          YTMyprocteDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
      }
      else
      {
          YTMyprocteDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
      }
      options.DbContextOptions.UseLoggerFactory(MyLoggerFactory);
      options.DbContextOptions.EnableSensitiveDataLogging(true);       //logging 不加密 development使用 !
});
(二)、.net Core 3.0及以上
1、在XXXXEntityFrameworkModule增加
 public static readonly ILoggerFactory MyLoggerFactory
    = LoggerFactory.Create(builder => { builder.AddConsole(); });
if (!SkipDbContextRegistration)
{
      Configuration.Modules.AbpEfCore().AddDbContext<SCMDbContext>(options =>
      {
          if (options.ExistingConnection != null)
          {
              SCMDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
          }
          else
          {
              SCMDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
          }
          options.DbContextOptions.UseLoggerFactory(MyLoggerFactory);
      });
}
posted @ 2021-10-18 18:48  悟自省  阅读(179)  评论(0编辑  收藏  举报