Log4net 根据日志类型输出日志

怎甘沉沦 提交于 2020-01-25 11:33:17

第一步:引入Log4net.dll 文件的引用

第二步:添加LogHelper类,代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;

namespace DemoLog4net
{
    /// <summary>
    /// 日志等级
    /// </summary>
    public enum LogLevel
    {
        Error,
        Debug,
        Warning,
        Info
    }
   /// <summary>
   /// 日志类型
   /// </summary>
    public enum LogType
    {
        AppLog,
        ErrorLog,
        DebugLog,
        OtherLog
    }
    /// <summary>
    /// 单例模式初始化
    /// </summary>
    public class Singleton
    {
        private ILog Log;
        private static Singleton instance;
        private Singleton() { }
        public static Singleton getInstance()
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
        /// <summary>
        /// 获取日志初始化器
        /// </summary>
        /// <param name="logType"></param>
        /// <returns></returns>
        public ILog Init(LogType logType)
        {
            string s = logType.ToString();
            Log = LogManager.GetLogger(s);
            return Log;
        }
    }
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 输出Erro日志
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="message"></param>
        public static void Error(LogType logType, string message)
        {
            WriteLog(logType, LogLevel.Error, message);
        }
        /// <summary>
        /// 输出Warning日志
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="message"></param>
        public static void Warning(LogType logType, string message)
        {
            //记录日志
            WriteLog(logType, LogLevel.Warning, message);
        }
        /// <summary>
        /// 输出Info日志
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="message"></param>
        public static void Info(LogType logType, string message)
        {
            //记录日志
            WriteLog(logType, LogLevel.Info, message);
        }
        public static void Debug(LogType logType, string message)
        {
            //记录日志
            WriteLog(logType, LogLevel.Debug, message);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="logLevel"></param>
        /// <param name="message"></param>
        private static void WriteLog(LogType logType, LogLevel logLevel, string message)
        {
            ILog Log = Singleton.getInstance().Init(logType);
            switch (logLevel)
            {
                case LogLevel.Debug:
                    Log.Debug(message);
                    break;
                case LogLevel.Error:
                    Log.Error(message);
                    break;
                case LogLevel.Info:
                    Log.Info(message);
                    break;
                case LogLevel.Warning:
                    Log.Warn(message);
                    break;
            }

        }
    }
}

 

第三步:添加配置文件Log4net.config文件 ,记得将此文件的【复制到输出目录 】设置复制

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <appSettings>
  </appSettings>
  <log4net>
    <!--一般信息日志-->
    <appender name="AppLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/AppLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="AppLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <!--严重错误日志-->
    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/ErrorLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="ErrorLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <!--调试日志-->
    <appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/DebugLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="DebugLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>

    <!--其他信息日志-->
    <appender name="OtherLog" type="log4net.Appender.RollingFileAppender">
      <file value="Log/OtherLog/" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout">
        <!--输出格式-->
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="OtherLog" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="AppLog"/>
      <appender-ref ref="ErrorLog"/>
      <appender-ref ref="DebugLog"/>
      <appender-ref ref="OtherLog"/>
    </root>
  </log4net>
</configuration>

第五步:在AssemblyInfo.cs文件中添加

 

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net_dcs.config", Watch = true)]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!