zoukankan      html  css  js  c++  java
  • C# web项目 log4net 使用

    1.在VS的管理Nuget中安装 log4net

    2.配置 webconfig

    在configurations 节点 插入如下代码:

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

    然后再配置完整的log4net信息:我将error与info信息分开记录

    <log4net>
        <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
        <!-- 设置日志类型,root节点比logger优先 -->
        <!--
        <root>
          <level value="ALL" />
          <appender-ref ref="info_log" />
        </root>
        -->
        <!-- 设置节点 -->
        <logger name="InfoLogger">
          <level value="INFO" />
          <appender-ref ref="info_log" />
        </logger>
        <logger name="ErrorLogger">
          <level value="ERROR" />
          <level value="WARN" />
          <appender-ref ref="error_log" />
        </logger>
        <!-- 节点信息明细设置 ,讲info与error分别记录-->
        <appender name="info_log" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="log4/" /> 
          <param name="AppendToFile" value="true" />
          <param name="RollingStyle" value="Date" />
          <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
          <param name="StaticLogFileName" value="false" />
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
        <appender name="error_log" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="log4/" /> 
          <param name="AppendToFile" value="true" />
          <param name="RollingStyle" value="Date" />
          <param name="DatePattern" value="&quot;Error_&quot;yyyyMMdd&quot;.txt&quot;" />
          <param name="StaticLogFileName" value="false" />
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>  
      </log4net>

    代码中的使用,使用2个帮助类:

     public static class LogFactory
        {
            /// <summary>
            /// 解析Xml的路径
            /// </summary>
            public const string Config = "~/Web.Config";
    
            /// <summary>
            /// 错误日志的标签
            /// </summary>
            public const string Error = "ErrorLogger";
    
            /// <summary>
            /// 日志记录的标签
            /// </summary>
            public const string Info = "InfoLogger";
    
            /// <summary>
            /// 初始化
            /// </summary>
            public static void LogInitialize()
            {
                log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(Config)));
            }
        }

    loghelper.cs

      /// <summary> 日志辅助类 log4net
        /// </summary>
        public static class LogHelper
        {
            /// <summary> 错误日志
            /// </summary>
            /// <param name="msg"></param>
            public static void Error(string msg)
            {
                var log = LogManager.GetLogger(LogFactory.Error);
                log.Error(msg);
            }
    
            /// <summary> 错误日志,带异常
            /// </summary>
            /// <param name="msg"></param>
            /// <param name="ex"></param>
            public static void Error(string msg, Exception ex)
            {           
                var log = LogManager.GetLogger(LogFactory.Error);
                log.Error(msg, ex);
            }
    
            /// <summary> 日志记录
            /// </summary>
            /// <param name="msg"></param>
            public static void Info(string msg)
            {           
               var log = LogManager.GetLogger(LogFactory.Info);
                log.Info(msg);
            }
    
            /// <summary> 日志记录
            /// </summary>
            /// <param name="msg"></param>
            /// <param name="ex"></param>
            public static void Info(string msg, Exception ex)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.Info(msg, ex);
            }
    
            /// <summary> Debug日志记录
            /// </summary>
            /// <param name="msg"></param>
            public static void Debug(string msg)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.Debug(msg);
            }
    
            /// <summary> Debug日志记录
            /// </summary>
            /// <param name="msg"></param>
            /// <param name="ex"></param>
            public static void Debug(string msg, Exception ex)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.Debug(msg, ex);
            }
    
            /// <summary> Format日志记录
            /// </summary>
            /// <param name="format"></param>
            /// <param name="arg0"></param>
            public static void Format(string format, object arg0)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.InfoFormat(format, arg0);
            }
    
            /// <summary> Format日志记录
            /// </summary>
            /// <param name="format"></param>
            /// <param name="arg0"></param>
            /// <param name="arg1"></param>
            public static void Format(string format, object arg0, object arg1)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.InfoFormat(format, arg0, arg1);
            }
    
            /// <summary> Format日志记录
            /// </summary>
            /// <param name="format"></param>
            /// <param name="arg0"></param>
            /// <param name="arg1"></param>
            /// <param name="arg2"></param>
            public static void Format(string format, object arg0, object arg1, object arg2)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.InfoFormat(format, arg0, arg1, arg2);
            }
    
            /// <summary> Format日志记录
            /// </summary>
            /// <param name="format"></param>
            /// <param name="provider"></param>
            /// <param name="args"></param>
            public static void Format(IFormatProvider provider, string format, params object[] args)
            {
                var log = LogManager.GetLogger(LogFactory.Info);
                log.InfoFormat(provider, format, args);
            }
        }
    View Code

    在Global.asax中配置初始化

    LogFactory.LogInitialize();

    测试页面调用:

       public ActionResult Login(LoginModel loginM)
            {           
                //返回结果类
                ResultModel rd = new ResultModel();
                //接收前端数据 
                string _account = loginM.Account;
                string _password = loginM.password;   
            
                //调用api验证获取返回值
                LogHelper.Info("调用api记录");       //记录日志
       
                //登陆成功,授权并记录         
                rd.Status = statuslist.成功;
                rd.ResultMessage = "数据已经收到";
                rd.ResulData = new string[] { _account, _password };//数组
                //赋予jsonresult data
                rd.Data = new { rd.Status, rd.ResultMessage, rd.ResulData };
                
           
             
                return rd;
            }
    View Code

     保存日志文件内容为乱码的情况,需要在log4net设置中添加:

     <param name="Encoding" value="utf-8" />

    即可。

  • 相关阅读:
    00072_System类
    eWebEditor实现word图片自动转存
    wangEditor实现word图片自动转存
    xhEditor实现word图片自动转存
    TinyMCE实现word图片自动转存
    FCKEditor实现word图片自动转存
    CKEditor实现word图片自动转存
    Nginx大文件(百M以上)的上传下载实现技术
    百度WebUploader大文件(百M以上)的上传下载实现技术
    SpringCloud大文件(百M以上)的上传下载实现技术
  • 原文地址:https://www.cnblogs.com/daniel-niu/p/10313344.html
Copyright © 2011-2022 走看看