NLog with DbUp

Thu, 25 Jun 2020 08:48 UTC by garethbrown

Implementing the IUpgradeLog for DbUp with NLog

using System;
using DbUp.Engine.Output;
using NLog;

namespace MyApp.DbDeploy
{
    public class NLogUpgradeLog : IUpgradeLog
    {
        public NLogUpgradeLog()
        {
            var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            string nlogConfigFileName;

            if (environmentName != null)
            {
                nlogConfigFileName = $"NLog.{environmentName}.config";
            }
            else
            {
                nlogConfigFileName = "NLog.config";
            }

            Logger = LogManager.LoadConfiguration(nlogConfigFileName).GetCurrentClassLogger();
        }

        public ILogger Logger { get; }

        public void WriteInformation(string format, params object[] args)
        {
            Logger.Info(string.Format(format, args));
        }

        public void WriteError(string format, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;

            Logger.Error(string.Format(format, args));

            Console.ResetColor();
        }

        public void WriteWarning(string format, params object[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            Logger.Warn(string.Format(format, args));

            Console.ResetColor();
        }
    }
}

// Use with
// upgradeEngine = DeployChanges.To
//                     .MySqlDatabase(upgradeInfo.DbConnectionString)
//                     .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), ScriptNameFilter)
//                     .LogTo(new NLogUpgradeLog())
//                     .Build();

The information on this site is provided “AS IS” and without warranties of any kind either
express or implied. To the fullest extent permissible pursuant to applicable laws, the author disclaims all warranties, express or implied, including, but not limited to, implied warranties of merchantability, non-infringement and suitability for a particular purpose.

UI block loader
One moment please ...