// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using log4net;
using Ringtoets.Common.Service.Properties;
namespace Ringtoets.Common.Service
{
///
/// This class defines helper methods for performing calculations.
///
public static class CalculationServiceHelper
{
private static readonly ILog log = LogManager.GetLogger(typeof(CalculationServiceHelper));
///
/// Logs messages as errors with formatting.
///
/// The format for the message.
/// The messages to log.
/// Thrown when any parameter
/// is null.
public static void LogMessagesAsError(string format, string[] errorMessages)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
if (errorMessages == null)
{
throw new ArgumentNullException(nameof(errorMessages));
}
foreach (string errorMessage in errorMessages)
{
log.ErrorFormat(format, errorMessage);
}
}
///
/// Logs messages as errors.
///
/// The messages to log.
/// Thrown when
/// is null.
public static void LogMessagesAsError(string[] errorMessages)
{
if (errorMessages == null)
{
throw new ArgumentNullException(nameof(errorMessages));
}
LogMessagesAsError("{0}", errorMessages);
}
///
/// Logs messages as warnings.
///
/// The messages to log.
/// Thrown when
/// is null.
public static void LogMessagesAsWarning(string[] warningMessages)
{
if (warningMessages == null)
{
throw new ArgumentNullException(nameof(warningMessages));
}
foreach (string waningMessage in warningMessages)
{
log.Warn(waningMessage);
}
}
///
/// Logs the begin of a validation action.
///
public static void LogValidationBegin()
{
log.Info(Resources.Validation_started);
}
///
/// Logs the end of a validation action.
///
public static void LogValidationEnd()
{
log.Info(Resources.Validation_ended);
}
///
/// Logs the begin of a calculation action.
///
public static void LogCalculationBegin()
{
log.Info(Resources.Calculation_started);
}
///
/// Logs the end of a calculation action.
///
public static void LogCalculationEnd()
{
log.Info(Resources.Calculation_ended);
}
///
/// Determines whether an error has occurred during the calculation.
///
/// The canceled state of the calculation.
/// Indicator if there is already an exception thrown in the calculation.
/// The contents of the last error file.
/// true when a calculation isn't canceled, has not already thrown an exception and
/// is set. false otherwise.
public static bool HasErrorOccurred(bool canceled, bool exceptionThrown, string lastErrorFileContent)
{
return !canceled && !exceptionThrown && !string.IsNullOrEmpty(lastErrorFileContent);
}
///
/// Logs message and exception as error.
///
/// The message to log.
/// The exception to log.
/// Thrown when any parameter
/// is null.
public static void LogExceptionAsError(string message, Exception exception)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
log.Error(message, exception);
}
}
}