静态日志框架Extensioner.Logging

静态日志框架,输出文本文件,基于log4net扩展,轻量,方便,无侵入!

使用方法

  1. 执行Install-Package Extensioner.Logging -Version 2.0.0导入Nuget包
  2. 导入log4net.config(示例在文章末尾)配置文件到应用目录下,修改文件属性为始终复制
  3. 程序中使用Log.Debug,Log.Info,Log.Error打印日志

示例

Log.Info("Hello World!");
Log.Debug("Hello World!");
Log.Error("Hello World!", new Exception("NETCore21,Hello World!"));

效果截图

log4net.config示例

点击查看代码
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
  </configSections>
  <!-- log4net日志配置 -->
  <log4net>
    <appender name="LogInfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Logs\NET5\log.info" />  <!-- 如果部署至linux则更改为linux路径 -->
      <appendToFile value="true" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="5000KB" />
      <param name="DatePattern" value=".yyyy-MM-dd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Composite" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="*************** Start *************** %newlineHostName:[%property{log4net:HostName}] %date - %message  %newline***************  End  ***************%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>
    <appender name="LogDebugFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Logs\NET5\log.debug" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="5000KB" />
      <param name="DatePattern" value=".yyyy-MM-dd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Composite" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="*************** Start *************** %newlineHostName:[%property{log4net:HostName}] %date - %message  %newline***************  End  ***************%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="DEBUG" />
      </filter>
    </appender>
    <appender name="LogErrorFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Logs\NET5\log.error" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="5000KB" />
      <param name="DatePattern" value=".yyyy-MM-dd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Composite" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="*************** Start *************** %newlineHostName:[%property{log4net:HostName}] %date - %message  %newline***************  End  ***************%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="ERROR" />
      </filter>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogInfoFileAppender" />
      <appender-ref ref="LogDebugFileAppender" />
      <appender-ref ref="LogErrorFileAppender" />
    </root>
    <logger name="Log4NetLogger">   <!--该节点不可变更-->
      <level value="ALL" />
    </logger>
  </log4net>
</configuration>

基础框架为.NET Standard 2.0,所以支持.NET Framework 4.6.1之后的所有.NET(包括Core和5)版本

posted @ 2021-05-28 15:40  Extensioner  阅读(78)  评论(0编辑  收藏  举报