首页 > 其他分享 >CSharp: donet 7 create logging File with EF Core 7.02

CSharp: donet 7 create logging File with EF Core 7.02

时间:2023-02-08 23:22:35浏览次数:50  
标签:donet logging 7.02 logStream Dispose optionsBuilder WriteLine DisposeAsync

 /// <summary>
        /// https://learn.microsoft.com/zh-cn/ef/core/logging-events-diagnostics/simple-logging
        /// </summary>
        /// <param name="optionsBuilder"></param>

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            //读取配置文件
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory()) //Environment.CurrentDirectory
                .AddJsonFile("appsettings.json")
                .AddInMemoryCollection()
                .Build();
            var connStr = config["ConnectionStrings:GeovinDuDbContext"];
            //日志
            //optionsBuilder.UseLoggerFactory(loggerFactory);
           optionsBuilder.LogTo(_logStream.WriteLine);
          //  optionsBuilder.LogTo(Console.WriteLine).EnableDetailedErrors();
          //  optionsBuilder.LogTo(Console.WriteLine,LogLevel.Debug,DbContextLoggerOptions.UtcTime | DbContextLoggerOptions.SingleLine);
            // optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovindu;User Id=sa;Password=geovindu;");

            optionsBuilder.UseSqlServer(connStr);
        }
        //日志文本
        private StreamWriter _logStream = new StreamWriter("geovindulog.txt", append: true);
        /// <summary>
        /// 
        /// </summary>
        public override void Dispose()
        {
            base.Dispose();
            _logStream.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override async ValueTask DisposeAsync()
        {
            await base.DisposeAsync();
            await _logStream.DisposeAsync();
        }

  

 

标签:donet,logging,7.02,logStream,Dispose,optionsBuilder,WriteLine,DisposeAsync
From: https://www.cnblogs.com/geovindu/p/17103692.html

相关文章