首页 > 代码库 > log4net配置

log4net配置

测试环境:vs2012
步骤1:下载log4net.dll;
步骤2:webconfig配置文件(log4net配置 ):
技术分享
 1   <!-- log4net配置 begin--> 2   <configSections> 3     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 4     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 5     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 6   </configSections> 7   <log4net> 8     <!--定义输出到文件中--> 9     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">10       <!--定义文件存放位置-->11       <file value="logs\\"/>12       <appendToFile value="true"/>13       <rollingStyle value="Date"/>14       <!--<datePattern value="http://www.mamicode.com/yyyy//yyyyMM//yyyyMMdd‘.txt‘"/>-->15       <datePattern value="yyyyMMdd‘.txt‘"/>16       <staticLogFileName value="false"/>17       <param name="MaxSizeRollBackups" value="100"/>18       <layout type="log4net.Layout.PatternLayout">19         <!--每条日志末尾的文字说明-->20         <!--输出格式-->21         <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->22         <!--<conversionPattern value="http://www.mamicode.com/%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%n"/>-->23         <!--<conversionPattern value="http://www.mamicode.com/%date:%message%n"/>-->24         <conversionPattern value="%n【记录时间】%date%n【描述】%message%n"/>25       </layout>26     </appender>27     <root>28       <!--文件形式记录日志-->29       <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->30       <level value="ALL"/>31       <appender-ref ref="RollingLogFileAppender"/>32       <!--<level value="http://www.mamicode.com/ERROR"/>33       --><!--文件形式记录日志--><!--34       <appender-ref ref="RollingLogFileAppender"/>35       <level value="http://www.mamicode.com/DEBUG"/>36       --><!--文件形式记录日志--><!--37       <appender-ref ref="RollingLogFileAppender"/>-->38     </root>39   </log4net>40   <startup>41     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>42   </startup>43   <!-- log4net配置 end-->
View Code
附上输出格式的注释:
%m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息
%n(new line):換行
%d(datetime):输出当前语句运行的时刻 
%r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数 
%t(thread id):当前语句所在的线程ID
%p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
%c(class):当前日志对象的名称
%L:输出语句所在的行号
%F:输出语句所在的文件名
%-数字:表示该项的最小长度,如果不够,则用空格填充

步骤3:添加log4net封装类:
技术分享
using System;using System.Collections.Generic;using System.Linq;using System.Web;[assembly: log4net.Config.XmlConfigurator(Watch = true)]namespace WebApp.Helper{    public class LogHelper    {        /// <summary>        /// 输出日志到Log4Net        /// </summary>        /// <param name="t"></param>        /// <param name="ex"></param>        #region static void WriteLog(Type t, Exception ex)        public static void WriteLog(Type t, Exception ex)        {            log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Error("Error", ex);        }        #endregion        /// <summary>        /// 输出日志到Log4Net        /// </summary>        /// <param name="t"></param>        /// <param name="msg"></param>        #region static void WriteLog(Type t, string msg)        public static void WriteLog(Type t, string msg)        {            log4net.ILog log = log4net.LogManager.GetLogger(t);            log.Error(msg);        }        #endregion        /// <summary>        /// 自定义写入日志        /// </summary>        /// <param name="msg"></param>        public static void WriteInfo(string msg)        {            log4net.ILog log = log4net.LogManager.GetLogger(typeof(LogHelper));            log.Error(msg);         }    }}
View Code

   步骤4:使用:

技术分享
            ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);            log.Debug("Debug");            log.Info("Info");             WebApp.Helper.LogHelper.WriteInfo("111111111111111");                         //DataCollection.Common.LogHelper.WriteLog("");            WebApp.Helper.LogHelper. WriteLog(typeof(Test), "测试Log4Net日志是否写入111111111111111");
View Code

 

 

提示:如果没有写入数据,主要原因是没有配置不正确;

 



 

  

log4net配置