LogHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  20. private static bool _hasBuild = false;
  21. public static void BuildLogger(bool usePID = true)
  22. {
  23. string outputTemplate;
  24. if (usePID)
  25. outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[线程={ThreadId}][PID={PID}][{Level:u3}]{Message:lj}{NewLine}\t{Exception}";
  26. else
  27. outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[线程={ThreadId}][{Level:u3}]{Message:lj}{NewLine}\t{Exception}";
  28. Serilog.Log.Logger = new Serilog.LoggerConfiguration()
  29. .Enrich.FromLogContext()
  30. .Enrich.With(new SerilogEnricher(usePID))
  31. .WriteTo.Console(outputTemplate: outputTemplate)
  32. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Information)
  33. .WriteTo.File("Logs\\Info\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  34. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Warning)
  35. .WriteTo.File("Logs\\Warning\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  36. .WriteTo.Logger(p => p.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
  37. .WriteTo.File("Logs\\Error\\.log", rollingInterval: Serilog.RollingInterval.Day, outputTemplate: outputTemplate))
  38. .CreateLogger();
  39. _hasBuild = true;
  40. }
  41. public static void Info(string msg)
  42. {
  43. if (!_hasBuild)
  44. BuildLogger();
  45. Serilog.Log.Information(msg);
  46. }
  47. public static void Warning(string msg, Exception ex = null)
  48. {
  49. if (!_hasBuild)
  50. BuildLogger();
  51. Serilog.Log.Warning(ex, msg);
  52. }
  53. public static void Error(string msg, Exception ex = null)
  54. {
  55. if (!_hasBuild)
  56. BuildLogger();
  57. Serilog.Log.Error(ex, msg);
  58. }
  59. }
  60. /// <summary>
  61. /// Serilog日志添加线程ID
  62. /// </summary>
  63. class SerilogEnricher : ILogEventEnricher
  64. {
  65. private bool useProcessId;
  66. private string PID;
  67. public SerilogEnricher(bool useProcessId = false)
  68. {
  69. this.useProcessId = useProcessId;
  70. if (useProcessId)
  71. {
  72. this.PID = Process.GetCurrentProcess().Id.ToString("D5");
  73. }
  74. }
  75. /// <summary>
  76. /// 在日志中添加线程ID
  77. /// </summary>
  78. /// <param name="logEvent"></param>
  79. /// <param name="propertyFactory"></param>
  80. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
  81. {
  82. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
  83. "ThreadId", Thread.CurrentThread.ManagedThreadId.ToString("D3")));
  84. if (useProcessId)
  85. {
  86. logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("PID", PID));
  87. }
  88. }
  89. }
  90. }