log4net下载地址 http://logging.apache.org/log4net/download_log4net.cgi

log4net-1.2.13-bin-newkey.zip(编译好的dll文件)

第1步,新建全局配置文件log4net.config,以每天日期格式分割日志文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL"/>
      <appender-ref ref="SysAppender"/>
    </root>

    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="ALL"/>
    </logger>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="Logger/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        <param name="Header" value="
----------------------header--------------------------
" />
        <param name="Footer" value="
----------------------footer--------------------------
" />
      </layout>
    </appender>
  </log4net>
</configuration>

第2步,web.config里<configuration>下添加这一行

<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

第3步,Properties目录下AssemblyInfo.cs

加上这一句[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

  

第4步,新建一个loghelp类

 public class logHelper
    {
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");

            public logHelper()
            {

            }

            private static void SetConfig()
            {
                object o = ConfigurationManager.GetSection("log4net");
                log4net.Config.XmlConfigurator.Configure(o as System.Xml.XmlElement);
            }

            public static void LogInfo(string Message)
            {
                if (!log.IsInfoEnabled)
                    SetConfig();
                log.Info(Message);
            }

            public static void LogInfo(string Message, Exception ex)
            {
                if (!log.IsInfoEnabled)
                    SetConfig();
                log.Info(Message, ex);
            }
            public static void ErrorInfo(string Message)
            {
                if (!log.IsInfoEnabled)
                    SetConfig();
                log.Error(Message);
            }

            public static void DebugInfo(string Message)
            {
                if (!log.IsInfoEnabled)
                    SetConfig();
                log.Debug(Message);
            }
    }

  第5步调用代码

logHelper.LogInfo("第"+sys.count+"步"+"用户名:" + UserInfo.UserName + "修改用户信息");

  第6步根据业务对日志文件进行分析(图片来自百度)

第7步

感谢 http://www.cnblogs.com/yeminglong/archive/2013/05/21/3091192.html

http://zhoufoxcn.blog.51cto.com/792419/429988/

posted on 2015-05-27 15:41  等风起  阅读(437)  评论(0编辑  收藏  举报