首页 > 代码库 > Log4net 根据日志类型输出日志
Log4net 根据日志类型输出日志
第一步:引入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".log"" /> <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".log"" /> <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".log"" /> <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".log"" /> <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)]
Log4net 根据日志类型输出日志
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。