Log.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Diagnostics;
  3. namespace Log
  4. {
  5. public static class Log
  6. {
  7. public static void Init(string logPath)
  8. {
  9. Debug.Listeners.Clear();
  10. Debug.Listeners.Add(new TextWriterTraceListener(logPath));
  11. Debug.AutoFlush = true;
  12. }
  13. public static void D(string message)
  14. {
  15. var sf = new StackFrame(1, true);
  16. string log = string.Format(
  17. "{0} {1} Line:{2} {3} [D]: {4}",
  18. System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  19. sf.GetFileName(),
  20. sf.GetFileLineNumber(),
  21. sf.GetMethod(),
  22. message
  23. );
  24. Debug.WriteLine(log);
  25. }
  26. public static void D(string message, params object[] args)
  27. {
  28. string info = String.Format(message, args);
  29. var sf = new StackFrame(1, true);
  30. string log = string.Format(
  31. "{0} {1} Line:{2} {3} [D]: {4}",
  32. System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  33. sf.GetFileName(),
  34. sf.GetFileLineNumber(),
  35. sf.GetMethod(),
  36. info
  37. );
  38. Debug.WriteLine(log);
  39. }
  40. public static void E(string message)
  41. {
  42. var sf = new StackFrame(1, true);
  43. string log = string.Format(
  44. "{0} {1} Line:{2} {3} [E]: {4}",
  45. System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  46. sf.GetFileName(),
  47. sf.GetFileLineNumber(),
  48. sf.GetMethod(),
  49. message
  50. );
  51. Debug.WriteLine(log);
  52. }
  53. public static void E(string message, params object[] args)
  54. {
  55. string info = String.Format(message, args);
  56. var sf = new StackFrame(1, true);
  57. string log = string.Format(
  58. "{0} {1} Line:{2} {3} [E]: {4}",
  59. System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  60. sf.GetFileName(),
  61. sf.GetFileLineNumber(),
  62. sf.GetMethod(),
  63. info
  64. );
  65. Debug.WriteLine(log);
  66. }
  67. }
  68. }