LogWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using log4net.Core;
  6. using System.Reflection;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Common.Util;
  9. using log4net.Appender;
  10. using log4net.Config;
  11. namespace Aitex.Core.RT.Log
  12. {
  13. class LogWriter
  14. {
  15. private readonly static Type ThisDeclaringType = typeof(LogWriter);
  16. private readonly ILogger defaultLogger;
  17. public LogWriter()
  18. {
  19. defaultLogger = log4net.Core.LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), "CommonLogger");
  20. }
  21. public void SetLogDirectory()
  22. {
  23. var _logFilePath = SC.GetStringValue("System.LogFilePath");
  24. if (PathManager.IsValidDirectoryPath(_logFilePath) && defaultLogger != null)
  25. {
  26. _logFilePath = _logFilePath.Trim('\\') + @"\log";
  27. Array.ForEach(defaultLogger.Repository.GetAppenders(), p =>
  28. {
  29. var appender = (RollingFileAppender)p;
  30. if (appender != null)
  31. {
  32. appender.File = _logFilePath;
  33. appender.ActivateOptions();
  34. BasicConfigurator.Configure(appender);
  35. }
  36. });
  37. LOG.Write($"RT launch:set log directory");
  38. }
  39. }
  40. string FormatLogString(LogItem logItem)
  41. {
  42. string fileName = string.Empty;
  43. string result = logItem.msg;
  44. try
  45. {
  46. //fileName = logItem.sf.GetFileName().Substring(logItem.sf.GetFileName().LastIndexOf('\\') + 1);
  47. //2014-10-17 17:53:51.9 INFO Log.cs Line:50 - Config模块初始化成功.
  48. var temp = logItem.sf.GetFileName();
  49. if (temp != null)
  50. {
  51. fileName = temp.Substring(temp.LastIndexOf('\\') + 1);
  52. }
  53. }
  54. catch (Exception)
  55. {
  56. fileName = string.Empty;
  57. }
  58. try
  59. {
  60. //result = string.Format("{0} LOG.{1} {2} Line:{3} {4}() - {5}", logItem.dt.ToString("yyyy/MM/dd HH:mm:ss.fff"),
  61. // logItem.lv.Name,
  62. // fileName,
  63. // logItem.sf.GetFileLineNumber().ToString(),
  64. // logItem.sf.GetMethod().Name,
  65. // logItem.msg);
  66. //result = string.Format("{0}\t{1}\t{2}\t{3}", logItem.dt.ToString("yyyy/MM/dd HH:mm:ss.fff"),
  67. // logItem.lv.Name,
  68. // logItem.StackFile,
  69. // logItem.msg);
  70. result = string.Format("{0}\t{1}\t{2} ", logItem.dt.ToString("yyyy/MM/dd HH:mm:ss.fff"),
  71. logItem.lv.Name,
  72. logItem.msg);
  73. }
  74. catch (Exception)
  75. {
  76. }
  77. return result;
  78. }
  79. public string Write(LogItem logItem)
  80. {
  81. string log = FormatLogString(logItem);
  82. if (defaultLogger.IsEnabledFor(logItem.lv))
  83. {
  84. defaultLogger.Log(typeof(LogWriter), logItem.lv, log, logItem.ex);
  85. }
  86. return log;
  87. }
  88. }
  89. public class TypedLogWriter
  90. {
  91. private readonly ILogger _logger;
  92. public TypedLogWriter(string type)
  93. {
  94. _logger = log4net.Core.LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), type);
  95. }
  96. public void Write(string text)
  97. {
  98. _logger.Log(typeof(TypedLogWriter), Level.Info, text, null);
  99. }
  100. }
  101. }