Is a method that writes a log to a log?

A pure method is defined as " not making any visible state changes. "

My method writes a log message if one argument is null or an exception is thrown. Is it still clean? Is a record in the registrar a visible change?

Here is the code:

    /// <summary>
    /// Formats with invariant culture - won't throw an exception.
    /// </summary>
    /// <param name="format">A composite format string.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.</returns>
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "CyanCor.Core.Logging.Log.ExceptionPrevent(System.String,System.String,System.Int32,System.String)", Justification = "It is for the logger (riedl)")]
    [Pure - yes or no?]
    public static string FormatSafe(this string format, params object[] args)
    {
        if (format == null)
        {
            Log.ExceptionPrevent("Argument format is null");
            return NullFormat;
        }

        try
        {
            return string.Format(CultureInfo.InvariantCulture, format, args);
        }
        catch (ArgumentException exc)
        {
            Log.Exception(exc);
            return format;
        }
        catch (FormatException exc)
        {
            Log.Exception(exc);
            return format;
        }
    }
+5
source share
1 answer

Generally, “state change” means a state change of an object, i.e. changing a variable or changing the structure of a complex object. Following this definition, it seems your method is still clean.

+5
source

All Articles