.net 5.0 注入中间件方式

引用

  • log4net
  • Microsoft.Extensions.Logging.Log4net.AspNetCore

log4.config【始终复制】

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <log4net>
  3. <!--指定日记记录方式,以滚动文件的方式(文件记录)-->
  4. <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  5. <!--日志路径-->
  6. <file value="Log\message" />
  7. <!--是否是向文件中追加日志-->
  8. <appendToFile value="true" />
  9. <!--log保留天数-->
  10. <param name= "MaxSizeRollBackups" value= "10"/>
  11. <!--每个文件最大1M-->
  12. <param name="maximumFileSize" value="1MB" />
  13. <!--日志根据日期滚动-->
  14. <param name="RollingStyle" value="Date" />
  15. <!--日志文件名格式为:logs_20080831.log-->
  16. <param name="DatePattern" value=""logs_"yyyyMMdd".log"" />
  17. <!--日志文件名是否是固定不变的-->
  18. <param name="StaticLogFileName" value="false" />
  19. <!--布局-->
  20. <layout type="log4net.Layout.PatternLayout">
  21. <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
  22. </layout>
  23. </appender>
  24. <root>
  25. <level value="ALL"/>
  26. <appender-ref ref="RollingFile" />
  27. </root>
  28. </log4net>

Program.cs

net50 - 图1

使用方式

  • 方式一

net50 - 图2

  • 方式二

net50 - 图3

控制台应用程序

引用包

  • log4net

log4net.config【始终复制】

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <!-- Level的级别,由高到低 -->
  4. <!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
  5. <!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
  6. <log4net>
  7. <!--错误日志类-->
  8. <logger name="error">
  9. <!--定义记录的日志级别-->
  10. <level value="ALL" />
  11. <!--输出到文件-->
  12. <appender-ref ref="ErrorAppender" />
  13. <!--输出到控制台-->
  14. <appender-ref ref="ConsoleAppender"/>
  15. </logger>
  16. <!--信息日志类-->
  17. <logger name="info">
  18. <level value="ALL" />
  19. <appender-ref ref="InfoAppender" />
  20. <appender-ref ref="ConsoleAppender"/>
  21. </logger>
  22. <!--调试日志类-->
  23. <logger name="debug">
  24. <level value="ALL" />
  25. <appender-ref ref="DebugAppender" />
  26. <appender-ref ref="ConsoleAppender"/>
  27. </logger>
  28. <!--错误日志附加介质:输出到文件-->
  29. <!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
  30. <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
  31. <!--日志输出到exe程序这个相对目录下-->
  32. <file value="Log\\Error\\" />
  33. <!--输出的日志不会覆盖以前的信息-->
  34. <appendToFile value="true" />
  35. <!--备份文件的个数-->
  36. <maxSizeRollBackups value="100" />
  37. <!--单个日志文件的最大大小-->
  38. <maxFileSize value="10240" />
  39. <!--是否使用静态文件名-->
  40. <staticLogFileName value="false" />
  41. <!--日志文件名-->
  42. <datePattern value="yyyyMMdd'.txt'" />
  43. <!--文件创建的方式,这里是以Date方式创建-->
  44. <rollingStyle value="Date" />
  45. <!--错误日志布局-->
  46. <layout type="log4net.Layout.PatternLayout">
  47. <conversionPattern value="%newline%date [Thread: %thread] %newline%message %newline"/>
  48. </layout>
  49. </appender>
  50. <!--信息日志附加介质:输出到文件-->
  51. <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
  52. <!--日志目录-->
  53. <file value="Log\\Info\\" />
  54. <appendToFile value="true" />
  55. <maxFileSize value="10240" />
  56. <maxSizeRollBackups value="100" />
  57. <!--日志文件名是否为静态-->
  58. <staticLogFileName value="false" />
  59. <datePattern value="yyyyMMdd'.txt'" />
  60. <rollingStyle value="Date" />
  61. <layout type="log4net.Layout.PatternLayout">
  62. <conversionPattern value="%newline%date [Thread: %thread] %message %newline"/>
  63. </layout>
  64. </appender>
  65. <!--调试日志附加介质:输出到文件-->
  66. <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
  67. <file value="Log\\Debug\\" />
  68. <appendToFile value="true" />
  69. <maxFileSize value="10240" />
  70. <maxSizeRollBackups value="100" />
  71. <staticLogFileName value="false" />
  72. <datePattern value="yyyyMMdd'.txt'" />
  73. <rollingStyle value="Date" />
  74. <layout type="log4net.Layout.PatternLayout">
  75. <conversionPattern value="%newline%date [Thread: %thread] %message %newline"/>
  76. </layout>
  77. </appender>
  78. <!--日志附加介质:输出到控制台-->
  79. <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  80. <!--<mapping>
  81. <level value="ERROR" />
  82. <foreColor value="White" />
  83. <backColor value="Red, HighIntensity" />
  84. </mapping>
  85. <mapping>
  86. <level value="DEBUG" />
  87. <foreColor value="White" />
  88. <backColor value="Green" />
  89. </mapping>
  90. <mapping>
  91. <level value="INFO" />
  92. <foreColor value="White" />
  93. --><!--<backColor value="Yellow" />--><!--
  94. </mapping>-->
  95. <layout type="log4net.Layout.PatternLayout">
  96. <!--%data格式为yyyy-MM-dd HH:mm:ss,sss-->
  97. <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss} [%-5level] %message%newline" />
  98. </layout>
  99. <filter type="log4net.Filter.LevelRangeFilter">
  100. <param name="LevelMin" value="Debug"/>
  101. <param name="LevelMax" value="Fatal"/>
  102. </filter>
  103. </appender>
  104. </log4net>
  105. </configuration>

控制台:LogHelper.cs

  1. using log4net;
  2. using log4net.Config;
  3. using log4net.Repository;
  4. using System.IO;
  5. public class LogHelper
  6. {
  7. private static ILoggerRepository repository { get; set; }
  8. private static ILog _logInfo;
  9. private static ILog _logError;
  10. private static ILog _logDebug;
  11. private static ILog logInfo
  12. {
  13. get
  14. {
  15. if (_logInfo == null)
  16. {
  17. Configure();
  18. }
  19. return _logInfo;
  20. }
  21. }
  22. private static ILog logError
  23. {
  24. get
  25. {
  26. if (_logError == null)
  27. {
  28. Configure();
  29. }
  30. return _logError;
  31. }
  32. }
  33. private static ILog logDebug
  34. {
  35. get
  36. {
  37. if (_logDebug == null)
  38. {
  39. Configure();
  40. }
  41. return _logDebug;
  42. }
  43. }
  44. public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
  45. {
  46. repository = LogManager.CreateRepository(repositoryName);
  47. XmlConfigurator.Configure(repository, new FileInfo(configFile));
  48. _logInfo = LogManager.GetLogger(repositoryName, "info");
  49. _logError = LogManager.GetLogger(repositoryName, "error");
  50. _logDebug = LogManager.GetLogger(repositoryName, "debug");
  51. }
  52. public static void Info(string msg)
  53. {
  54. logInfo.Info(msg);
  55. }
  56. public static void Debug(string msg)
  57. {
  58. logError.Debug(msg);
  59. }
  60. public static void Error(string msg)
  61. {
  62. logError.Error(msg);
  63. }
  64. }