DbUp has a simple logging abstraction in place using the IUpgradeLog interface. Out of the box there are the following loggers:
ConsoleUpgradeLog- Logs toConsole.Write*.- Use
builder.LogToConsole()to register.
- Use
TraceUpgradeLog- Logs toTrace.Write*.- Use
builder.LogToTrace()to register.
- Use
SqlContextUpgradeLog- Logs toSqlContext(available when using thedbup-sqlserverpackage on .NET Framework).- Use
builder.LogToSqlContext()to register.
- Use
- Use
builder.LogTo(new MyCustomLogger())to provide your own logger based onIUpgradeLog. - Use
builder.LogTo(ILogger)orbuilder.LogTo(ILoggerFactory)to provide a Microsoft.Extensions.Logging compliant logging provider.
These calls use builder.Configure((UpgradeConfiguration c) => c.AddLog(log)) under the covers.
If no logger is specified, no logging is enabled.
The first call to upgradeConfiguration.AddLog(log) will replace the default logger. Subsequent calls to upgradeConfiguration.AddLog(log) will combine the loggers and result in logs going to all specified loggers. To clear previously configured loggers call builder.ResetConfiguredLoggers().
By default, the output of the scripts run by DbUp do not show up in the logs. To also display the script output (e.g.: text displayed by PRINT statements in Sql Server), use builder.LogScriptOutput():
Builder
.LogToConsole()
.LogScriptOutput()
Complete example:
DeployChanges.To
.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.LogToConsole()
.LogScriptOutput()
.Build()
Third-party logging provider example using Serilog:
// Create the logging provider instance.
var loggerFactory = new LoggerFactory().AddSerilog(
new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger());
DeployChanges.To
.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.LogTo(loggerFactory) // Register the logging provider.
.LogScriptOutput()
.Build()