/// <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