.net错误日志记录(log4)

Log4

web.config

<!--这段放前面-->
<configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>


<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Log\LogDataList\\"/>
      <!--file可以指定具体的路径 eg : d:\\test.log。不指定的话log被生成在项目的bin/Debug 或者 bin/Release目录下 (web的项目 默认生成在根目录下)-->
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd/&quot;log.log&quot;"/>
      <param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock"/>
      <maxSizeRollBackups value="1000"/>
      <!--备份log文件的个数最多10个-->
      <maximumFileSize value="5MB"/>
      <!--每个log文件最大是2M,如果超过2M将重新创建一个新的log文件,并将原来的log文件备份。-->
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--指定log的格式  [%M] 方法名称   -->
        <conversionPattern value="-----------------------------------------------------------------------------%newline[日期:%d] %newline  日志级别:%-5level,   发生在类:%c[%M] 、%L行,%newline  描述:%m  %newline"/>
      </layout>
    </appender>
    <root>
      <!--<level value="DUBEG" />-->
      <!--指定将此级别及以上的log打印到log文件中-->
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string CurrIP = "";
            try
            {

                CurrIP = System.Web.HttpContext.Current.Request.UserHostAddress;
                int i = int.Parse("tt");
            }
            catch (Exception ex)
            {
                //写入日志
                string msg = "消费发生异常请及时处理【BAL.PayConsume】:\n strNumber:\n  strNumber:\n goodsNumber:结果rsult:\n ip:" + CurrIP + "\n 时间:" + DateTime.Now.ToString("yyyyMMdd-hhmmss");
                dalcomd.wLog(LogLevel.Error, msg, ex);
            }
            finally
            {
                //写入日志
                string msg = "消费记录【BAL.PayConsume】:\n strNumber:\n  strNumber:\n goodsNumber:结果rsult:\n ip:" + CurrIP + "\n 时间:" + DateTime.Now.ToString("yyyyMMdd-hhmmss");
                dalcomd.wLog(LogLevel.Info, msg);
            }
        }
    }
}

 

using System;
using System.Data;
using System.Text;
using log4net;
using System.IO;


[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace WebApplication1
{
    public static class dalcomd
    {

        /// <summary>
        /// 日志类
        /// </summary>
        private static ILog log
        {
            get
            {
                Type declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
                ILog log = log4net.LogManager.GetLogger(declaringType);
                return log;
            }
        }


        /// <summary>
        /// 写入运行日志
        /// </summary>
        /// <param name="Errorlevel">日志级别</param>
        /// <param name="ErrorMsg">错误描述</param>
        /// <param name="ex">异常</param>
        /// <param name="age">参数列表</param>
        public static void wLog(LogLevel Errorlevel, string ErrorMsg, params string[] age)
        {
            wLog(Errorlevel, ErrorMsg, new Exception(), age);
        }

        /// <summary>
        /// 写入运行日志
        /// </summary>
        /// <param name="Errorlevel">日志级别</param>
        /// <param name="ErrorMsg">错误描述</param>
        /// <param name="ex">异常</param>
        /// <param name="age">参数列表</param>
        public static void wLog(LogLevel Errorlevel, string ErrorMsg)
        {
            wLog(Errorlevel, ErrorMsg, new Exception());
        }


        /// <summary>
        /// 写入运行日志
        /// </summary>
        /// <param name="Errorlevel">日志级别</param>
        /// <param name="ErrorMsg">错误描述</param>
        /// <param name="ex">异常</param>
        public static void wLog(LogLevel Errorlevel, string ErrorMsg, Exception ex)
        {
            ILog log = dalcomd.log;
            switch (Errorlevel)
            {
                case LogLevel.Debug: log.Debug(ErrorMsg, ex); break;
                case LogLevel.Info: log.Info(ErrorMsg, ex); break;
                case LogLevel.Warn: log.Warn(ErrorMsg, ex); break;
                case LogLevel.Error: log.Error(ErrorMsg, ex); break;
                case LogLevel.Fatal: log.Fatal(ErrorMsg, ex); break;
            }
        }


        /// <summary>
        /// 写入运行日志
        /// </summary>
        /// <param name="Errorlevel">日志级别</param>
        /// <param name="ErrorMsg">错误描述</param>
        /// <param name="ex">异常</param>
        /// <param name="age">参数列表</param>
        public static void wLog(LogLevel Errorlevel, string ErrorMsg, Exception ex, params string[] age)
        {
            ILog log = dalcomd.log;
            StringBuilder SBParams = new StringBuilder();
            foreach (string str in age)
            {
                SBParams.Append(str + ",");
            }
            ErrorMsg = ErrorMsg + "\n{参数:[" + SBParams.ToString() + "]}";
            switch (Errorlevel)
            {
                case LogLevel.Debug: log.Debug(ErrorMsg, ex); break;
                case LogLevel.Info: log.Info(ErrorMsg, ex); break;
                case LogLevel.Warn: log.Warn(ErrorMsg, ex); break;
                case LogLevel.Error: log.Error(ErrorMsg, ex); break;
                case LogLevel.Fatal: log.Fatal(ErrorMsg, ex); break;
            }
        }


        /// <summary>
        /// 自定义方法写入日志
        /// </summary>
        /// <param name="FilePath">路径</param>
        /// <param name="FileName">文件名</param>
        /// <param name="content">内容</param>
        public static void LogResult(string FilePath, string FileName, string content)
        {
            try
            {
                //目录
                string strFilePath = FilePath + "//" + DateTime.Now.ToString("yyyyMMdd");
                //完整文件地址
                string AllFilePath = strFilePath + "//" + FileName;
                if (!Directory.Exists(strFilePath))
                {
                    Directory.CreateDirectory(strFilePath);
                }

                StreamWriter write = new StreamWriter(AllFilePath, true, System.Text.Encoding.UTF8);
                write.Write(content);
                write.WriteLine();
                write.Close();
                write.Dispose();
            }
            catch (Exception ex)
            {
                string msg = FilePath + "------" + FileName + "-----" + content;
                wLog(LogLevel.Error, "自定义日志方写入异常:" + msg, ex);
            }
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebApplication1
{
    /// <summary>
    ///  LOG日志记录等级枚举
    /// </summary>
    public enum LogLevel
    {
        /// <summary>
        /// Debug信息记录 调试信息
        /// </summary>
        Debug = 0,

        /// <summary>
        /// Info信息记录 记录信息
        /// </summary>
        Info = 1,

        /// <summary>
        /// Warn信息记录 警告信息
        /// </summary>
        Warn = 2,

        /// <summary>
        /// Error信息记录 普通错误
        /// </summary>
        Error = 3,

        /// <summary>
        /// Fatal信息记录 致命错误
        /// </summary>
        Fatal = 4
    }
}

DownLoad .dll File

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。