LogWriter.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace Aitex.Core.RT.Log
  8. {
  9. class LogWriter
  10. {
  11. private readonly static Type ThisDeclaringType = typeof(LogWriter);
  12. private readonly ILogger defaultLogger;
  13. public LogWriter()
  14. {
  15. defaultLogger = log4net.Core.LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), "CommonLogger");
  16. }
  17. string FormatLogString(LogItem logItem)
  18. {
  19. string fileName = string.Empty;
  20. string result = logItem.msg;
  21. try
  22. {
  23. fileName = logItem.sf.GetFileName().Substring(logItem.sf.GetFileName().LastIndexOf('\\') + 1);
  24. //2014-10-17 17:53:51.9 INFO Log.cs Line:50 - Config模块初始化成功。
  25. }
  26. catch (Exception)
  27. {
  28. fileName = string.Empty;
  29. }
  30. try
  31. {
  32. result = string.Format("{0} LOG.{1} {2} Line:{3} {4}() - {5}", logItem.dt.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  33. logItem.lv.Name,
  34. fileName,
  35. logItem.sf.GetFileLineNumber().ToString(),
  36. logItem.sf.GetMethod().Name,
  37. logItem.msg);
  38. }
  39. catch (Exception)
  40. {
  41. }
  42. return result;
  43. }
  44. public string Write(LogItem logItem)
  45. {
  46. string log = FormatLogString(logItem);
  47. if (defaultLogger.IsEnabledFor(logItem.lv))
  48. {
  49. defaultLogger.Log(typeof(LogWriter), logItem.lv, log, logItem.ex);
  50. }
  51. return log;
  52. }
  53. }
  54. }