[英]How do I direct log output to a file, using a FluentMigrator v3.2.15 In-Process runner?
I have a .NET 4.5.2 migration runner built using FluentMigrator 2.0.7 that "m trying to move to .NET 5.0 and FluentMigrator 3.2.15.我有一个使用 FluentMigrator 2.0.7 构建的 .NET 4.5.2 迁移运行器,“我正在尝试迁移到 .NET 5.0 和 FluentMigrator 3.2.15。
My current difficulty is writing the output to a text file.我目前的困难是将 output 写入文本文件。
var serviceProvider = new ServiceCollection()
.AddSingleton<ILoggerProvider, SqlScriptFluentMigratorLoggerProvider>()
.Configure<LogFileFluentMigratorLoggerOptions>(o => {
o.OutputFileName = "MyFilename.log";
})
.AddLogging(lb => lb.AddFluentMigratorConsole())
.Configure<FluentMigratorLoggerOptions>(o =>
{
o.ShowSql = true;
o.ShowElapsedTime = true;
})
.AddFluentMigratorCore()
.ConfigureRunner(builder =>
builder
.AddSqlServer2016()
.WithGlobalConnectionString(this.options.connectionString.ExpandDataDirectory())
.WithMigrationsIn(this.assembly)
)
.BuildServiceProvider();
using (var scope = serviceProvider.CreateScope())
{
var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
if (this.options.reverseMigration)
runner.MigrateDown(0);
else
runner.MigrateUp();
}
My problem is simple - when I try to run the migration I get an error:我的问题很简单 - 当我尝试运行迁移时出现错误:
Unable to resolve service for type 'System.IO.TextWriter' while attempting to activate 'FluentMigrator.Runner.Logging.SqlScriptFluentMigratorLoggerProvider'.尝试激活“FluentMigrator.Runner.Logging.SqlScriptFluentMigratorLoggerProvider”时无法解析“System.IO.TextWriter”类型的服务。
What's going on is simple enough - TextWriter is an abstract class, it can't be initiated.发生的事情很简单——TextWriter 是一个抽象的 class,它无法启动。
But how do I configured the ServiceProvider so that when it's asked for a TextWriter, it returns a StreamWriter writing to the OutputFileName I provided to LogFileFluentMigratorLoggerOptions?但是如何配置 ServiceProvider,以便在要求 TextWriter 时,它返回一个 StreamWriter,写入我提供给 LogFileFluentMigratorLoggerOptions 的 OutputFileName?
=== ===
Edited:编辑:
I can do this:我可以做这个:
using var logStream = new FileStream(this.options.outputFilename, FileMode.Append);
using var sw = new StreamWriter(logStream);
var serviceProvider = new ServiceCollection()
.AddSingleton<TextWriter>(sw)
.AddSingleton<ILoggerProvider, SqlScriptFluentMigratorLoggerProvider>()
...
But it strikes me as being very ugly...但是我觉得很丑。。。
I could write an sql output to a text file by registering LogFileFluentMigratorLoggerProvider as a singleton and then configuring it.我可以通过将 LogFileFluentMigratorLoggerProvider 注册为 singleton 来将 sql output 写入文本文件,然后对其进行配置。 LogFileFluentMigratorLoggerProvider inherits from SqlScriptFluentMigratorLoggerProvider and takes care to instantiate a StreamWriter for you. LogFileFluentMigratorLoggerProvider 继承自 SqlScriptFluentMigratorLoggerProvider 并负责为您实例化 StreamWriter。
private static IServiceProvider CreateServices()
{
// var connectionString = ""; grab your connection string
return new ServiceCollection()
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer2016()
.WithGlobalConnectionString(connectionString)
.WithMigrationsIn(this.assembly))
.AddLogging(lb => lb.AddFluentMigratorConsole())
.AddSingleton<ILoggerProvider, LogFileFluentMigratorLoggerProvider>()
.Configure<LogFileFluentMigratorLoggerOptions>(
opt =>
{
opt.OutputFileName = "C:\\TEMP\\DatabaseMigration.sql";
opt.OutputGoBetweenStatements = true;
opt.ShowSql = true;
})
.BuildServiceProvider(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.