static void ConfigureLogging(Arguments arguments)
{
var writeActions = new List<Action<string>>
{
s => log.AppendLine(s)
};
if (arguments.Output == OutputType.BuildServer || arguments.LogFilePath == "console" || arguments.Init)
{
writeActions.Add(Console.WriteLine);
}
Exception exception = null;
if (arguments.LogFilePath != null && arguments.LogFilePath != "console")
{
try
{
var logFileFullPath = Path.GetFullPath(arguments.LogFilePath);
var logFile = new FileInfo(logFileFullPath);
// NOTE: logFile.Directory will be null if the path is i.e. C:\logfile.log. @asbjornu
if (logFile.Directory != null)
{
logFile.Directory.Create();
}
using (logFile.CreateText())
{
}
writeActions.Add(x => WriteLogEntry(arguments, x));
}
catch (Exception ex)
{
exception = ex;
}
}
Logger.SetLoggers(
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Debug) a(s); }),
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Info) a(s); }),
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Warn) a(s); }),
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Error) a(s); }));
if (exception != null)
Logger.WriteError(string.Format("Failed to configure logging for '{0}': {1}", arguments.LogFilePath, exception.Message));
}