zoukankan      html  css  js  c++  java
  • Log4Net根据不同的Logger名称,生成日志文件到不同的地方。

    1.定义日志记录类

       1:   public class Log4NetLogger : ISystemLogger
       2:      {
       3:          static log4net.ILog securityLogger = log4net.LogManager.GetLogger("SecurityLog");
       4:          static log4net.ILog sysLogger = log4net.LogManager.GetLogger("SystemLog");
       5:   
       6:          public SystemLogLevel LogLevel
       7:          {
       8:              get
       9:              {
      10:                  if (ApplicationManager.SystemInfo.SystemLogLevel.HasValue)
      11:                  {
      12:                      return (SystemLogLevel)ApplicationManager.SystemInfo.SystemLogLevel;
      13:                  }
      14:                  else
      15:                  {
      16:                      return SystemLogLevel.None;
      17:                  }
      18:              }
      19:              set
      20:              {
      21:                  ApplicationManager.UpdateLogLevel(value);
      22:              }
      23:          }
      24:   
      25:          /// <summary>
      26:          /// 输出错误级别日志
      27:          /// </summary>
      28:          /// <param name="message">输出的消息</param>
      29:          public void Error(string message)
      30:          {
      31:              //记录日志
      32:              writeSysLog(SystemLogLevel.Error, message);
      33:          }
      34:   
      35:          /// <summary>
      36:          /// 输出警告级别日志
      37:          /// </summary>
      38:          /// <param name="message">输出的消息</param>
      39:          public void Warning(string message)
      40:          {
      41:              //记录日志
      42:              writeSysLog(SystemLogLevel.Warning, message);
      43:          }
      44:   
      45:          /// <summary>
      46:          /// 输出信息级别日志
      47:          /// </summary>
      48:          /// <param name="message">输出的消息</param>
      49:          public void Info(string message)
      50:          {
      51:              //记录日志
      52:              writeSysLog(SystemLogLevel.Info, message);
      53:          }
      54:   
      55:          /// <summary>
      56:          /// 输出调试级别日志
      57:          /// </summary>
      58:          /// <param name="message">输出的消息</param>
      59:          public void Debug(string message)
      60:          {
      61:              //记录日志
      62:              writeSysLog(SystemLogLevel.Debug, message);
      63:          }
      64:   
      65:          /// <summary>
      66:          /// 记录个资法案日志
      67:          /// </summary>
      68:          public void SecurityLog(string message)
      69:          {
      70:              writeSecurityLog(message);
      71:          }
      72:   
      73:          /// <summary>
      74:          /// 记录系统日志
      75:          /// </summary>
      76:          /// <param name="logLevel">日志级别</param>
      77:          /// <param name="message">输出的消息</param>
      78:          private void writeSysLog(SystemLogLevel logLevel, string message)
      79:          {
      80:              string format = "ClientIP={0},CurrentUser={1},Message={2}";
      81:              ClientInfo clientInfo = WebServerHelper.CurrentClientInfo;
      82:              string fullMsg = string.Format(format, clientInfo.UserHostAddress, WebServerHelper.CurrentUserID, message);
      83:              if (LogLevel >= logLevel)
      84:              {
      85:                  switch (logLevel)
      86:                  {
      87:                      case SystemLogLevel.Debug:
      88:                          sysLogger.Debug(fullMsg);
      89:                          break;
      90:                      case SystemLogLevel.Error:
      91:                          sysLogger.Error(fullMsg);
      92:                          break;
      93:                      case SystemLogLevel.Info:
      94:                          sysLogger.Info(fullMsg);
      95:                          break;
      96:                      case SystemLogLevel.Warning:
      97:                          sysLogger.Warn(fullMsg);
      98:                          break;
      99:                  }
     100:              }
     101:          }
     102:          /// <summary>
     103:          /// 记录个资法案日志
     104:          /// </summary>
     105:          /// <param name="message"></param>
     106:          private void writeSecurityLog(string message)
     107:          {
     108:              string format = "ClientIP={0},CurrentUser={1},System={2},Message={3}";
     109:              ClientInfo clientInfo = WebServerHelper.CurrentClientInfo;
     110:              string fullMsg = string.Format(format, clientInfo.UserHostAddress, WebServerHelper.CurrentUserID,ApplicationManager.SystemCode, message);
     111:              securityLogger.Info(fullMsg);
     112:          }
     113:      }

    2.设置配制文件:

       1:  <configSections>        
       2:          <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
       3:  </configSections>
       4:  <log4net>
       5:          <appender name="SystemLogAppender" type="log4net.Appender.RollingFileAppender">
       6:              <file value="OA_Log/App_Log/" />
       7:              <appendToFile value="true" />
       8:              <rollingStyle value="Date" />
       9:              <staticLogFileName value="false" />
      10:              <datePattern value="yyyyMMdd&quot;.log&quot;" />
      11:              <layout type="log4net.Layout.PatternLayout">
      12:                  <!--输出格式-->
      13:                  <conversionPattern value="%date %-5level %message%newline" />
      14:              </layout>
      15:              <filter type="log4net.Filter.LoggerMatchFilter">
      16:                  <loggerToMatch value="SystemLog" />               
      17:              </filter>
      18:              <filter type="log4net.Filter.DenyAllFilter" />
      19:          </appender>
      20:          <appender name="SecurityLogAppender" type="log4net.Appender.RollingFileAppender">
      21:              <file value="OA_Log/Access_Log/" />
      22:              <appendToFile value="true" />
      23:              <rollingStyle value="Date" />
      24:              <staticLogFileName value="false" />
      25:              <datePattern value="yyyyMMdd&quot;.log&quot;" />
      26:              <layout type="log4net.Layout.PatternLayout">
      27:                  <!--输出格式-->
      28:                  <conversionPattern value="%date %-5level %message%newline" />
      29:              </layout>
      30:              <filter type="log4net.Filter.LoggerMatchFilter">
      31:                  <loggerToMatch value="SecurityLog" />               
      32:              </filter>
      33:              <filter type="log4net.Filter.DenyAllFilter" />
      34:          </appender>        
      35:          <root>
      36:              <level value="ALL"/>            
      37:              <appender-ref ref="SystemLogAppender"/>
      38:              <appender-ref ref="SecurityLogAppender"/>            
      39:          </root>       
      40:  </log4net>

    3.在Global.asax的Application_Start事件中加入初始化代码:

       1:   protected void Application_Start(object sender, EventArgs e)
       2:   {
       3:              log4net.Config.XmlConfigurator.Configure();         
       4:   }
  • 相关阅读:
    华为摄像机sdc开发_02_华为摄像机到手后的一些问题
    华为摄像机sdc开发_01_基础开发环境搭建
    LNMP环境搭建及服务器加固
    常用数据库端口号
    Linux文件查找
    SpringBoot使用jackson
    Vue.js源码解析-Vue初始化流程之动态创建DOM
    Vue.js源码解析-Vue初始化流程
    Vue.js源码解析-从scripts脚本看vue构建
    Linux系统编程-文件IO
  • 原文地址:https://www.cnblogs.com/leleroyn/p/3558843.html
Copyright © 2011-2022 走看看