From 1ec531ab3c0d8df5f8bca664b7fd1c6a20442cac Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Sat, 17 Feb 2018 22:36:13 +1000 Subject: [PATCH] Fixed AutodetectUpgradeLog when no logger was found. Wrote to console instead. --- src/DbUp.sln.DotSettings | 4 +++ .../Engine/Output/AutodetectUpgradeLog.cs | 26 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/DbUp.sln.DotSettings b/src/DbUp.sln.DotSettings index 1563583e..2c78c0a3 100644 --- a/src/DbUp.sln.DotSettings +++ b/src/DbUp.sln.DotSettings @@ -1,5 +1,9 @@  DO_NOT_SHOW + Implicit + Implicit SQ + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <data><IncludeFilters /><ExcludeFilters /></data> <data /> \ No newline at end of file diff --git a/src/dbup-core/Engine/Output/AutodetectUpgradeLog.cs b/src/dbup-core/Engine/Output/AutodetectUpgradeLog.cs index 543c83c2..afb57aff 100644 --- a/src/dbup-core/Engine/Output/AutodetectUpgradeLog.cs +++ b/src/dbup-core/Engine/Output/AutodetectUpgradeLog.cs @@ -1,17 +1,41 @@ #if SUPPORTS_LIBLOG +using System; using DbUp.Engine.Output.LibLog; namespace DbUp.Engine.Output { public class AutodetectUpgradeLog : IUpgradeLog { - private readonly Logger log = LogProvider.ForceResolveLogProvider().GetLogger("DbUp"); + private readonly Logger log = LogProvider.ForceResolveLogProvider()?.GetLogger("DbUp") + ?? LogToConsoleInstead; public void WriteInformation(string format, params object[] args) => log(LogLevel.Info, () => format, null, args); public void WriteError(string format, params object[] args) => log(LogLevel.Error, () => format, null, args); public void WriteWarning(string format, params object[] args) => log(LogLevel.Warn, () => format, null, args); + + static bool LogToConsoleInstead(LogLevel level, Func format, Exception exception, object[] args) + { + ConsoleColor GetColor() + { + switch (level) + { + case LogLevel.Warn: + return ConsoleColor.Yellow; + case LogLevel.Fatal: + case LogLevel.Error: + return ConsoleColor.Red; + default: + return ConsoleColor.White; + } + } + + Console.ForegroundColor = GetColor(); + Console.WriteLine(format(), args); + Console.ResetColor(); + return true; + } } } #endif \ No newline at end of file