欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

log4net使用封装,无缝切换 dotnet 和 dotnetcore

程序员文章站 2022-04-28 11:13:26
自己有几个自己的小项目,有许多公用的方法/扩展/工具类等等,于是封装了一些常用的工具类/扩展方法做了一个类库 WeihanLi.Common,日志使用了自己比较常用的 log4net,开始默认使用的log4net进行处理日志,在1.0.12版本之前直接依赖 log4net,后来觉得这样做不太好,一是... ......

log4net使用封装,无缝切换 dotnet 和 dotnetcore

intro

自己有几个自己的小项目,有许多公用的方法/扩展/工具类等等,于是封装了一些常用的工具类/扩展方法 weihanli.common,日志使用了自己比较常用的 log4net,开始默认使用的log4net进行处理日志,在1.0.12版本之前直接依赖 log4net,后来觉得这样做不太好,一是多了 log4net 这一依赖而且有点一个公共类库依赖某一个具体的日志框架有点不妥,后来决定把 log4net 给独立出来,单独迁移出来一个 weihanli.common.logging.log4net

本文主要介绍一下,怎么通过 weihanli.common.logging.log4net 来使用 log4net,让你在 .net core 应用中有着和 .net framework 一致的体验

安装

通过 nuget 安装 weihanli.common.logging.log4net

使用

  1. log4net 初始化

    log4nethelper.loginit(); // 默认使用当前目录的 log4net.config 作为配置文件
    
    log4nethelper.loginit(log4netconfigfilepath); // 手动指定 log4net 配置文件路径
  2. 获取 logger,getlogger

    var genericlogger = log4nethelper.getlogger<classname>();
    
    var typelogger = log4nethelper.getlogger(typeof(program));
    
    var logger = log4nethelper.getlogger("loggername");
  3. 记录日志

    logger.debug(msg);
    logger.info(msg);
    logger.info(msg, exception);
    logger.warn(msg);
    logger.warn(msg, exception);
    logger.error(msg);
    logger.error(msg, exception);
    logger.fatal(msg);
    logger.fatal(msg, exception);
  4. 配置使用 elasticsearchappender

    log4net 示例配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configsections>
    <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" />
  </configsections>
  <log4net>
    <appender name="rollinglogfileappender" type="log4net.appender.rollingfileappender">
      <file type="log4net.util.patternstring" value="./log/systemlog.%date{yyyy-mm-dd}.log" />
      <appendtofile value="true" />
      <encoding value="unicodefffe" />
      <rollingstyle value="date" />
      <datepattern value="yyyymmdd" />
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="adonetappender" type="log4net.appender.adonetappender">
      <buffersize value="100" />
      <connectiontype value="system.data.sqlclient.sqlconnection, system.data" />
      <connectionstring value="data source=.;initial catalog=weblog;integrated security=false;persist security info=true;user id=[***];password=[***]" />
      <commandtext value="insert into [dbo].[testlog]([date],[thread],[level],[logger],[message],[exception]) values(@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parametername value="@log_date" />
        <dbtype value="datetime" />
        <layout type="log4net.layout.rawtimestamplayout" />
      </parameter>
      <parameter>
        <parametername value="@thread" />
        <dbtype value="string" />
        <size value="255" />
        <layout type="log4net.layout.patternlayout">
          <conversionpattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parametername value="@log_level" />
        <dbtype value="string" />
        <size value="50" />
        <layout type="log4net.layout.patternlayout">
          <conversionpattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parametername value="@logger" />
        <dbtype value="string" />
        <size value="255" />
        <layout type="log4net.layout.patternlayout">
          <conversionpattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parametername value="@message" />
        <dbtype value="string" />
        <size value="4000" />
        <layout type="log4net.layout.patternlayout">
          <conversionpattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parametername value="@exception" />
        <dbtype value="string" />
        <size value="2000" />
        <layout type="log4net.layout.exceptionlayout" />
      </parameter>
      <filter type="log4net.filter.levelrangefilter">
        <!--<param name="levelmin" value="debug" />
        <param name="levelmax" value="error" />-->
        <param name="levelmin" value="warn" />
      </filter>
    </appender>
    <appender name="traceappender" type="log4net.appender.traceappender">
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger [%property{ndc}] - %message%newline" />
      </layout>
    </appender>
    <appender name="aspnettraceappender" type="log4net.appender.aspnettraceappender">
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger [%property{ndc}] - %message%newline" />
      </layout>
    </appender>
    <appender name="consoleappender" type="log4net.appender.consoleappender">
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="coloredconsoleappender" type="log4net.appender.coloredconsoleappender">
      <mapping>
        <level value="error" />
        <forecolor value="white" />
        <backcolor value="red, highintensity" />
      </mapping>
      <mapping>
        <level value="debug" />
        <backcolor value="green" />
      </mapping>
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="smtpappender" type="log4net.appender.smtpappender">
      <authentication value="basic" />
      <username value="ben121011@126.com" />
      <password value="*******" />
      <to value="w***@outlook.com" />
      <from value="ben121011@126.com" />
      <subject value="logging message test" />
      <smtphost value="smtp.126.com" />
      <buffersize value="512" />
      <!--  超长是否丢弃 -->
      <lossy value="true" />
      <!-- 下面的定义, 就是 日志级别 大于 error 的, 才发邮件.  -->
      <filter type="log4net.filter.levelrangefilter">
        <levelmin value="error" />
        <levelmax value="fatal" />
      </filter>
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%newline %date  %newline [%thread] %newline %-5level %logger  %newline - %message" />
      </layout>
    </appender>
    <appender name="elasticsearchappender" type="weihanli.common.logging.log4net.elasticsearchappender, weihanli.common.logging.log4net">
      <buffersize value="2" />
      <elasticsearchurl value="http://localhost:9200" />
    </appender>
    <root>
      <level value="all" />
      <appender-ref ref="traceappender" />
      <appender-ref ref="consoleappender" />
      <appender-ref ref="rollinglogfileappender" />
      <!--
      <appender-ref ref="coloredconsoleappender" />
      <appender-ref ref="adonetappender" />
      <appender-ref ref="smtpappender" />
      <appender-ref ref="elasticsearchappender" />
      -->
    </root>
  </log4net>
</configuration>

memo

如果有什么问题或建议,欢迎提出