C# web项目 log4net 使用

2021-06-18 07:03

阅读:406

标签:err   ret   val   action   man   日志   data   file   etc   

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

技术分享图片

2.配置 webconfig

在configurations 节点 插入如下代码:

 
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

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

"InfoLogger">
      "INFO" />
      ref ref="info_log" />
    "ErrorLogger">
      "ERROR" />
      "WARN" />
      ref ref="error_log" />
    "info_log" type="log4net.Appender.RollingFileAppender">
      "File" value="log4/" /> 
      "AppendToFile" value="true" />
      "RollingStyle" value="Date" />
      "DatePattern" value=""Logs_"yyyyMMdd".txt"" />
      "StaticLogFileName" value="false" />
      "log4net.Layout.PatternLayout,log4net">
        "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      "error_log" type="log4net.Appender.RollingFileAppender">
      "File" value="log4/" /> 
      "AppendToFile" value="true" />
      "RollingStyle" value="Date" />
      "DatePattern" value=""Error_"yyyyMMdd".txt"" />
      "StaticLogFileName" value="false" />
      "log4net.Layout.PatternLayout,log4net">
        "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      

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

 public static class LogFactory
    {
        /// 
        /// 解析Xml的路径
        /// 
        public const string Config = "~/Web.Config";

        /// 
        /// 错误日志的标签
        /// 
        public const string Error = "ErrorLogger";

        /// 
        /// 日志记录的标签
        /// 
        public const string Info = "InfoLogger";

        /// 
        /// 初始化
        /// 
        public static void LogInitialize()
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(Config)));
        }
    }

loghelper.cs

技术分享图片技术分享图片
  ///  日志辅助类 log4net
    /// 
    public static class LogHelper
    {
        ///  错误日志
        /// 
        /// 
        public static void Error(string msg)
        {
            var log = LogManager.GetLogger(LogFactory.Error);
            log.Error(msg);
        }

        ///  错误日志,带异常
        /// 
        /// 
        /// 
        public static void Error(string msg, Exception ex)
        {           
            var log = LogManager.GetLogger(LogFactory.Error);
            log.Error(msg, ex);
        }

        ///  日志记录
        /// 
        /// 
        public static void Info(string msg)
        {           
           var log = LogManager.GetLogger(LogFactory.Info);
            log.Info(msg);
        }

        ///  日志记录
        /// 
        /// 
        /// 
        public static void Info(string msg, Exception ex)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.Info(msg, ex);
        }

        ///  Debug日志记录
        /// 
        /// 
        public static void Debug(string msg)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.Debug(msg);
        }

        ///  Debug日志记录
        /// 
        /// 
        /// 
        public static void Debug(string msg, Exception ex)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.Debug(msg, ex);
        }

        ///  Format日志记录
        /// 
        /// 
        /// 
        public static void Format(string format, object arg0)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.InfoFormat(format, arg0);
        }

        ///  Format日志记录
        /// 
        /// 
        /// 
        /// 
        public static void Format(string format, object arg0, object arg1)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.InfoFormat(format, arg0, arg1);
        }

        ///  Format日志记录
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void Format(string format, object arg0, object arg1, object arg2)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.InfoFormat(format, arg0, arg1, arg2);
        }

        ///  Format日志记录
        /// 
        /// 
        /// 
        /// 
        public static void Format(IFormatProvider provider, string format, params object[] args)
        {
            var log = LogManager.GetLogger(LogFactory.Info);
            log.InfoFormat(provider, format, args);
        }
    }
View Code

使用

技术分享图片技术分享图片
   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

 

C# web项目 log4net 使用

标签:err   ret   val   action   man   日志   data   file   etc   

原文地址:https://www.cnblogs.com/daniel-niu/p/10313344.html


评论


亲,登录后才可以留言!