log4

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <!--日志记录组建配置-->
  <log4net>

    <logger name="ExceptionLogger">
      <level value="ERROR" />
      <appender-ref ref="ExceptionFileAppender" />
      <!--<appender-ref ref="ConsoleAppender" />-->
    </logger>

    <logger name="InfoLogger">
      <level value="INFO" />
      <appender-ref ref="InfoFileAppender" />
      <!--<appender-ref ref="ConsoleAppender" />-->
    </logger>

    <logger name="DebugLogger">
      <level value="All" />
      <appender-ref ref="DebugFileAppender" />
      <!--<appender-ref ref="ConsoleAppender" />-->
    </logger>

    <!-- Exception -->
    <appender name="ExceptionFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\Exception\" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'..log'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="------------------------------------------------------------&#xD;&#xA;" />
        <ConversionPattern value="&#xD;&#xA;%date [%thread] - %message%newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR"/>
        <param name="LevelMax" value="ERROR"/>
      </filter>
    </appender>

    <!-- Info -->
    <appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\Info\" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'..log'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="------------------------------------------------------------&#xD;&#xA;" />
        <ConversionPattern value="%date [%thread] - %message%newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO"/>
        <param name="LevelMax" value="INFO"/>
      </filter>
    </appender>

    <!-- Debug -->
    <appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\Debug\" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'..log'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="------------------------------------------------------------&#xD;&#xA;" />
        <ConversionPattern value="%date [%thread] - %message%newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="DEBUG"/>
      </filter>
    </appender>

    <!-- Console部分log输出格式的设定 -->
    <!--<appender name="ConsoleAppender" type="log4net.Appender.RollingFileAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] - %message%newline%newline" />
      </layout>
    </appender>-->

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <!--<level value="ALL" />-->
      <appender-ref ref="ExceptionFileAppender" />
      <appender-ref ref="InfoFileAppender" />
      <appender-ref ref="DebugFileAppender" />
    </root>
  </log4net>
</configuration>

logger

    public static class Logger
    {
        static ILog log;

        static Logger()
        {
            string strPath = System.AppDomain.CurrentDomain.BaseDirectory;
            strPath = System.IO.Path.Combine(strPath, "log4net.config");
            System.IO.FileInfo log4File = new System.IO.FileInfo(strPath);
            log4net.Config.XmlConfigurator.Configure(log4File); 
            log = LogManager.GetLogger("AdoNet");
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="argContent"></param>
        public static void LogInfo(string argContent)
        {
            log.Info(argContent);
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="argObj"></param>
        public static void LogInfo(object argObj)
        {
            log.Info(new { argObj });
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void LogError(string message,System.Exception exception)
        {
            log.Error(message, exception);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void LogError( object message,System.Exception exception)
        {
            log.Error(new {message = message, exception});
        }


        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="error"></param>
        public static void LogError(string error)
        {
            log.Error(new {error = error});
        }
    }

调用


Logger.LogError("Error?");
Logger.LogInfo("Info");

你可能感兴趣的:(log4)