Log.cs 3.04 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vrh.OneReport.Lib.Areas.OneReport.Helpers
{
    public class Log
    {
        /// <summary>
        /// The available levels of logging.
        /// </summary>
        public enum LogLevel
        {
            None,
            Error,
            Warning,
            Info,
            Debug
        }
#if (DEBUG)
        private static LogLevel Level = LogLevel.Debug;
        private static LogLevel DebugLevel = LogLevel.Debug;
#else
		private static LogLevel Level = LogLevel.Error;
		private static LogLevel DebugLevel = LogLevel.Error;
#endif
        public static System.Web.Mvc.ModelStateDictionary modelstate;

        private static List<string> TextToDisplay = new List<string>();

        /// <summary>
        /// Sets the log level.
        /// <param name="level">The new log level.</param>
        /// </summary>
        public static void SetLogLevel(LogLevel level)
        {
            Level = level;
        }

        /// <summary>
        /// Logs an error message.
        /// <param name="text">The text to be logged.</param>
        /// </summary>
        public static void Error(string text)
        {
            if (Level >= LogLevel.Error)
            {
                modelstate.AddModelError(string.Empty, text);
            }
        }

        /// <summary>
        /// Logs a warning message.
        /// <param name="text">The text to be logged.</param>
        /// </summary>
        public static void Warning(string text)
        {
            if (Level >= LogLevel.Warning)
            {
                if (DebugLevel >= LogLevel.Warning)
                    TextToDisplay.Add("Warning: " + text);
            }
        }

        /// <summary>
        /// Logs an info message.
        /// <param name="text">The text to be logged.</param>
        /// </summary>
        public static void Info(string text)
        {
            if (Level >= LogLevel.Info)
            {
                if (DebugLevel >= LogLevel.Info)
                    TextToDisplay.Add(text);
            }
        }

        /// <summary>
        /// Logs a debug message.
        /// <param name="text">The text to be logged.</param>
        /// </summary>
        public static void Debug(string text)
        {
            if (Level >= LogLevel.Debug)
            {
                if (DebugLevel >= LogLevel.Debug)
                    TextToDisplay.Add("Debug: " + text);
            }
        }

        /// <summary>
        /// Gets the html message.
        /// <return>The text in html.</returns>
        /// </summary>
        public static string GetHtmlMessages()
        {
            string rv = "";

            foreach (var item in TextToDisplay)
            {
                if (string.IsNullOrEmpty(rv))
                    rv = "<ul>";
                rv += "<li>" + item + "</li>";
            }
            if (!string.IsNullOrEmpty(rv))
                rv += "</ul>";
            TextToDisplay.Clear();

            return rv;
        }
    }
}