PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/InternalTrace.cs

#
C# | 110 lines | 80 code | 18 blank | 12 comment | 11 complexity | 57a00c24d23178ced080db67510604be MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. namespace NUnit.Core
  8. {
  9. /// <summary>
  10. /// InternalTraceLevel is an enumeration controlling the
  11. /// level of detailed presented in the internal log.
  12. /// </summary>
  13. public enum InternalTraceLevel
  14. {
  15. Default,
  16. Off,
  17. Error,
  18. Warning,
  19. Info,
  20. Verbose
  21. }
  22. /// <summary>
  23. /// Summary description for Logger.
  24. /// </summary>
  25. public class InternalTrace
  26. {
  27. private readonly static string TIME_FMT = "HH:mm:ss.fff";
  28. private static bool initialized;
  29. private static InternalTraceWriter writer;
  30. public static InternalTraceWriter Writer
  31. {
  32. get { return writer; }
  33. }
  34. public static InternalTraceLevel Level;
  35. public static void Initialize(string logName)
  36. {
  37. int lev = (int) new System.Diagnostics.TraceSwitch("NTrace", "NUnit internal trace").Level;
  38. Initialize(logName, (InternalTraceLevel)lev);
  39. }
  40. public static void Initialize(string logName, InternalTraceLevel level)
  41. {
  42. if (!initialized)
  43. {
  44. Level = level;
  45. if (writer == null && Level > InternalTraceLevel.Off)
  46. {
  47. writer = new InternalTraceWriter(logName);
  48. writer.WriteLine("InternalTrace: Initializing at level " + Level.ToString());
  49. }
  50. initialized = true;
  51. }
  52. }
  53. public static void Flush()
  54. {
  55. if (writer != null)
  56. writer.Flush();
  57. }
  58. public static void Close()
  59. {
  60. if (writer != null)
  61. writer.Close();
  62. writer = null;
  63. }
  64. public static Logger GetLogger(string name)
  65. {
  66. return new Logger( name );
  67. }
  68. public static Logger GetLogger( Type type )
  69. {
  70. return new Logger( type.FullName );
  71. }
  72. public static void Log(InternalTraceLevel level, string message, string category)
  73. {
  74. Log(level, message, category, null);
  75. }
  76. public static void Log(InternalTraceLevel level, string message, string category, Exception ex)
  77. {
  78. Writer.WriteLine("{0} {1,-5} [{2,2}] {3}: {4}",
  79. DateTime.Now.ToString(TIME_FMT),
  80. level == InternalTraceLevel.Verbose ? "Debug" : level.ToString(),
  81. #if NET_2_0
  82. System.Threading.Thread.CurrentThread.ManagedThreadId,
  83. #else
  84. AppDomain.GetCurrentThreadId(),
  85. #endif
  86. category,
  87. message);
  88. if (ex != null)
  89. Writer.WriteLine(ex.ToString());
  90. }
  91. }
  92. }