LogHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Serilog;
  2. using Serilog.Core;
  3. using Serilog.Events;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace XdCxRhDW.Framework
  14. {
  15. public static class LogHelper
  16. {
  17. static LogHelper()
  18. {
  19. AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
  20. {
  21. string path1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns");
  22. string path2 = AppDomain.CurrentDomain.BaseDirectory;
  23. string dll1 = Path.Combine(path1, args.Name.Split(',')[0] + ".dll");
  24. string dll2 = Path.Combine(path2, args.Name.Split(',')[0] + ".dll");
  25. if (File.Exists(dll1))
  26. {
  27. return Assembly.LoadFrom(dll1);
  28. }
  29. if (File.Exists(dll2))
  30. {
  31. return Assembly.LoadFrom(dll2);
  32. }
  33. return null;
  34. };
  35. }
  36. private static bool _hasBuild = false;
  37. public static void BuildLogger(bool usePID = true)
  38. {
  39. string outputTemplate;
  40. if (usePID)
  41. outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[线程={ThreadId}][PID={PID}][{Level:u3}]{Message:lj}{NewLine}\t{Exception}";
  42. else
  43. outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[线程={ThreadId}][{Level:u3}]{Message:lj}{NewLine}\t{Exception}";
  44. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  45. .Enrich.FromLogContext()
  46. .Enrich.With(new SerilogEnricher(usePID))
  47. .WriteTo.Console(outputTemplate: outputTemplate)
  48. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  49. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  50. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  51. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  52. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  53. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  54. .CreateLogger();
  55. _hasBuild = true;
  56. }
  57. public static void Info(string msg)
  58. {
  59. if (!_hasBuild)
  60. BuildLogger();
  61. Serilog.Log.Information(msg);
  62. }
  63. public static void Warning(string msg, Exception ex = null)
  64. {
  65. if (!_hasBuild)
  66. BuildLogger();
  67. Serilog.Log.Warning(ex, msg);
  68. }
  69. public static void Error(string msg, Exception ex = null)
  70. {
  71. if (!_hasBuild)
  72. BuildLogger();
  73. Serilog.Log.Error(ex, msg);
  74. }
  75. }
  76. /// <summary>
  77. /// Serilog日志添加线程ID
  78. /// </summary>
  79. class SerilogEnricher : ILogEventEnricher
  80. {
  81. private bool useProcessId;
  82. private string PID;
  83. public SerilogEnricher(bool useProcessId = false)
  84. {
  85. this.useProcessId = useProcessId;
  86. if (useProcessId)
  87. {
  88. this.PID = Process.GetCurrentProcess().Id.ToString("D5");
  89. }
  90. }
  91. /// <summary>
  92. /// 在日志中添加线程ID
  93. /// </summary>
  94. /// <param name="logEvent"></param>
  95. /// <param name="propertyFactory"></param>
  96. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
  97. {
  98. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
  99. "ThreadId", Thread.CurrentThread.ManagedThreadId.ToString("D3")));
  100. if (useProcessId)
  101. {
  102. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("PID", PID));
  103. }
  104. }
  105. }
  106. }