/// <summary> /// Runs the delay for the given caller. Obtains a new <see cref="_LastTimedMessageID"/>. If, after the duration, the value /// didn't change, removes the message (using <see cref="RemoveLog(int)"/>)s /// </summary> /// <param name="loggerID"></param> /// <param name="duration"></param> /// <returns></returns> private async Task MessageDelayAsync(int loggerID, InfoLoggerMessageDuration duration) { // If the duration is infinite simply return if (duration == InfoLoggerMessageDuration.Infinite) { return; } // Get a unique ID var messageID = _RandomNumberGenerator.Next(); // Assign it to the controling property _LastTimedMessageID = messageID; // Wait for the duration of the delay await Task.Delay(_DefinedDurations[duration]); // If the ID remained the same if (messageID == _LastTimedMessageID) { // Remove the log RemoveLog(loggerID); // And reset the last id _LastTimedMessageID = 0; } }
/// <summary> /// Logs a new message. The message has to contain at least one non-white space character to be logged. To remove messages /// use <see cref="RemoveLog(int)"/> /// </summary> /// <param name="message"></param> /// <param name="loggerID">ID used to identify the caller. Messages may be removed only if the same ID is used later</param> /// <param name="duration">Maximum lifetime of the message, if the message was still presented after the time expires, /// it will be removed. By default it's infinite</param> public void Log(string message, int loggerID, InfoLoggerMessageDuration duration = InfoLoggerMessageDuration.Infinite) { // Log the message Log(message, loggerID); // Run the delay task MessageDelayAsync(loggerID, duration); }
/// <summary> /// Logs a new message anonymously (the message won't be removable by the means of <see cref="RemoveLog(int)"/>. /// The message has to contain at least one non-white space character to be logged. To remove messages /// use <see cref="RemoveLog(int)"/> /// </summary> /// <param name="message"></param> /// <param name="duration">Maximum lifetime of the message, if the message was still presented after the time expires, /// it will be removed. By default it's infinite</param> public void Log(string message, InfoLoggerMessageDuration duration = InfoLoggerMessageDuration.Infinite) => // Log an anonymous message, generate a random id for the caller (necessary so as if there are multiple limited time // messages they won't be removed by previous tasks completing (which would happen if an identical ID was passed everytime) Log(message, _RandomNumberGenerator.Next(), duration);
/// <summary> /// Shortcut to log anonymous messages through <see cref="IInfoLogger"/> /// </summary> /// <param name="message"></param> /// <param name="duration">Maximum lifetime of the message, if the message was still presented after the time expires, /// it will be removed. By default it's infinite</param> public static void Log(string message, InfoLoggerMessageDuration duration = InfoLoggerMessageDuration.Infinite) => Container.Resolve <IInfoLogger>().Log(message, duration);