Blame view

Vrh.Web.OneReport.Lib/Helpers/Log.cs 3.04 KB
ab9f2fbe   Schwirg László   Add project files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  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;
          }
      }
  }