(精华)2020年8月17日 C#基础知识点 Log4日志的用法

(精华)2020年8月17日 C#基础知识点 Log4日志的用法

nuget安装log4net包

配置文件代码如下

<?xml version="1.0" encoding="utf-8"?>
<log4net>
    <!-- Define some output appenders -->
    <appender name="rollingAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log\log.txt" />

        <!--追加日志内容-->
        <appendToFile value="true" />

        <!--防止多线程时不能写Log,官方说线程非安全-->
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

        <!--可以为:Once|Size|Date|Composite-->
        <!--Composite为Size和Date的组合-->
        <rollingStyle value="Composite" />

        <!--当备份文件时,为文件名加的后缀-->
        <datePattern value="yyyyMMdd.TXT" />

        <!--日志最大个数,都是最新的-->
        <!--rollingStyle节点为Size时,只能有value个日志-->
        <!--rollingStyle节点为Composite时,每天有value个日志-->
        <maxSizeRollBackups value="20" />

        <!--可用的单位:KB|MB|GB-->
        <maximumFileSize value="3MB" />

        <!--置为true,当前最新日志文件名永远为file节中的名字-->
        <staticLogFileName value="true" />

        <!--输出级别在INFO和ERROR之间的日志-->
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="FATAL" />
        </filter>

        <!--必须结合起来用,第一个只过滤出WARN,第二个拒绝其它其它日志输出-->
        <!--
        <filter type="log4net.Filter.LevelMatchFilter">
            <param name="LevelToMatch" value="WARN" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />-->

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
        </layout>
    </appender>

  <!--nhiberate和nhiberateSQL日志-->
    <!--<appender name="nhibernateAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log\nhiberate.txt"/>
        <appendToFile value="true"/>
        <maximumFileSize value="100KB"/>
        <rollingStyle value="Size"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%d{HH:mm:ss} [%t] %-5p %c - %m%n"/>
        </layout>
    </appender>

    <appender name="nhibernateSQLAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log\nhibernateSQL.txt"/>
        <appendToFile value="true" />
        <rollingStyle value="Composite" />
        <datePattern value="yyyyMMdd" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="1MB" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%d{HH:mm:ss} [%t] %-5p %c - %m%n"/>
        </layout>
    </appender>-->

    <!-- levels: OFF > FATAL > ERROR > WARN > INFO > DEBUG  > ALL -->
    <root>
        <priority value="ALL"/>
        <level value="ALL"/>
        <appender-ref ref="rollingAppender" />
    </root>

    <!--<logger name="NHibernate" >
        <level value="ERROR"/>
        <appender-ref ref="nhibernateAppender"/>
    </logger>

    <logger name="NHibernate.SQL">
        <level value="DEBUG"/>
        <appender-ref ref="nhibernateSQLAppender" />
        <appender-ref ref="trace" />
    </logger>-->

</log4net>

创建log4的日志类

public class Logger
{<!-- -->
   static Logger()
   {<!-- -->
       XmlConfigurator.Configure(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CfgFiles\\log4net.config")));
       ILog Log = LogManager.GetLogger(typeof(Logger));
       Log.Info("系统初始化Logger模块");
   }

   private ILog loger = null;
   public Logger(Type type)
   {<!-- -->
       loger = LogManager.GetLogger(type);
   }

   /// <summary>
   /// Log4日志
   /// </summary>
   /// <param name="msg"></param>
   /// <param name="ex"></param>
   public void Error(string msg = "出现异常", Exception ex = null)
   {<!-- -->
       Console.WriteLine(msg);
       loger.Error(msg, ex);
   }

   /// <summary>
   /// Log4日志
   /// </summary>
   /// <param name="msg"></param>
   public void Warn(string msg)
   {<!-- -->
       Console.WriteLine(msg);
       loger.Warn(msg);
   }

   /// <summary>
   /// Log4日志
   /// </summary>
   /// <param name="msg"></param>
   public void Info(string msg)
   {<!-- -->
       Console.WriteLine(msg);
       loger.Info(msg);
   }

   /// <summary>
   /// Log4日志
   /// </summary>
   /// <param name="msg"></param>
   public void Debug(string msg )
   {<!-- -->
       Console.WriteLine(msg);
       loger.Debug(msg);
   }


}

使用

private Logger logger = new Logger(typeof(LuceneQuery));
posted @ 2020-12-30 11:53  不要摸我的腰  阅读(295)  评论(0编辑  收藏  举报