SharePoint【学习笔记】-- 系统日志Log4net应用

使用强大的开源日志库Log4net来处理应用程序的日志信息,记录错误,警告,信息。

1 下载Log4net,在项目中引用log4net.dll

2 在AssemblyInfo.cs中添加以下代码

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

3 添加cs文件

复制代码
public static class Log4netUtil { private static ILog GetLog() { ILog log = null; try { StackTrace stack = new StackTrace(); MethodBase method = stack.GetFrame(8).GetMethod(); string methodName = method.DeclaringType.Namespace + "." 

                                  + method.DeclaringType.Name + "." 

                                  + method.ToString().Substring(method.ToString().IndexOf(" ") + 1); log = LogManager.GetLogger(methodName); } catch (Exception ex) { throw ex; } return log; } /// <summary>

        /// Fatals log /// </summary>

        /// <param name="pMethod">method</param>

        /// <param name="pMessage">message</param>

        public static void Fatal(string pMessage) { SPSecurity.RunWithElevatedPrivileges(delegate() { ILog log = Log4netUtil.GetLog(); if (log.IsFatalEnabled) { log.Fatal(pMessage); //SPListLog.Log.Fatal(pMessage);

 } }); } /// <summary>

        /// Erros log /// </summary>

        /// <param name="pMethod">method.</param>

        /// <param name="pMessage">message.</param>

        public static void Error(string pMessage) { SPSecurity.RunWithElevatedPrivileges(delegate() { ILog log = Log4netUtil.GetLog(); if (log.IsErrorEnabled) { log.Error(pMessage); //SPListLog.Log.Error(pMessage);

 } }); } /// <summary>

        /// Warning log /// </summary>

        /// <param name="pMethod">method.</param>

        /// <param name="pMessage">message.</param>

        public static void Warn(string pMessage) { SPSecurity.RunWithElevatedPrivileges(delegate() { ILog log = Log4netUtil.GetLog(); if (log.IsWarnEnabled) { log.Warn(pMessage); //SPListLog.Log.Warn(pMessage);

 } }); } /// <summary>

        /// Information log /// </summary>

        /// <param name="pMethod">method.</param>

        /// <param name="pMessage">message.</param>

        public static void Info(string pMessage) { SPSecurity.RunWithElevatedPrivileges(delegate() { ILog log = Log4netUtil.GetLog(); if (log.IsInfoEnabled) { log.Info(pMessage); //SPListLog.Log.Info(pMessage);

 } }); } /// <summary>

        /// Debugging log /// </summary>

        /// <param name="pMethod">method.</param>

        /// <param name="pMessage">message.</param>

        public static void Debug(string pMessage) { SPSecurity.RunWithElevatedPrivileges(delegate() { ILog log = Log4netUtil.GetLog(); if (log.IsDebugEnabled) { log.Debug(pMessage); //SPListLog.Log.Debug(pMessage);

 } }); } }
复制代码

4 添加log4net.config文件,其内容如下

复制代码
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <!--log configuration-->

  <configSections>

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

  </configSections>

  <!--site log configuraiton-->

  <log4net>

    <root>

      <!-- Level: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->

      <priority value="ALL"/>

      <appender-ref ref="RollingFileAppender"/>

    </root>

    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date %-5level %logger %message%newline"/>

      </layout>

    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date %-5level %logger %message%newline"/>

      </layout>

    </appender>

    <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >

      <bufferSize value="5" />

      <lossy value="true" />

      <evaluator type="log4net.Core.LevelEvaluator">

        <threshold value="WARN"/>

      </evaluator>

      <appender-ref ref="ConsoleAppender" />

    </appender>

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">

      <file value="Log\\"/>

      <appendToFile value="true"/>

      <rollingStyle value="Composite" />

      <datePattern value="yyyyMMdd&quot;_log.txt&quot;" />

      <maxSizeRollBackups value="10" />

      <maximumFileSize value="50MB" />

      <staticLogFileName value="false"/>

      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

      <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date %-5level %logger %message%newline"/>

      </layout>

    </appender>

  </log4net>

</configuration>
复制代码

5 使用log4net,日志将被记录在bin\log文件夹下。

你可能感兴趣的:(SharePoint)