自定义log日志

自定义log日志_第1张图片

 

自定义log日志_第2张图片

 

Log.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;

namespace PC.Common
{
    public class Log
    {
        //在网站根目录下创建日志目录
        public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs";

        /**
         * 向日志文件写入调试信息
         * @param className 类名
         * @param content 写入内容
         */
        public static void Debug(string className, string content)
        {
            if (LogLevel.LOG_LEVENL >= 3)
            {
                WriteLog("DEBUG", className, content);
            }
        }

        /**
        * 向日志文件写入运行时信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Info(string className, string content)
        {
            if (LogLevel.LOG_LEVENL >= 2)
            {
                WriteLog("INFO", className, content);
            }
        }

        /**
        * 向日志文件写入出错信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Error(string className, string content)
        {
            if (LogLevel.LOG_LEVENL >= 1)
            {
                WriteLog("ERROR", className, content);
            }
        }

        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        */
        protected static void WriteLog(string type, string className, string content)
        {
            if (!Directory.Exists(path))//如果日志目录不存在就创建
            {
                Directory.CreateDirectory(path);
            }


            string time = DateTime.Now.ToString("HH:mm:ss.fff");//获取当前系统时间
            string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名

            //创建或打开日志文件,向日志文件末尾追加记录
            StreamWriter mySw = File.AppendText(filename);

            DateTime now = DateTime.Now;


            if (now.Hour > 0 && now.Hour < 8)
            {
                //向日志文件写入内容
                string write_content = "「凌晨」" + time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
            }
            else if (now.Hour > 8 && now.Hour < 12)
            {
                //向日志文件写入内容
                string write_content = "【上午】" + time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
            }
            else if (now.Hour > 12 && now.Hour < 18)
            {
                //向日志文件写入内容
                string write_content = "『下午』" + time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
            }
            else if (now.Hour > 18 && now.Hour < 24)
            {
                //向日志文件写入内容
                string write_content = "〖晚上〗" + time + " " + type + " " + className + ": " + content;
                mySw.WriteLine(write_content);
            }

            //关闭日志文件
            mySw.Close();
        }
    }
}

 

LogLevel.cs

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

namespace PC.Common
{
    public class LogLevel
    {
        public static string AppKey(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息 
        /// </summary>

        public static int LOG_LEVENL
        {
            get
            {
                string log_levenl = "0";
                if (AppKey("log_leven") != "")
                {
                    log_levenl = AppKey("log_leven");
                }
                return Convert.ToInt32(log_levenl);
            }
        }
    }
}

 

web.config

image

 

 

调用方式

Log.Debug(this.GetType().ToString(), "json : " + json);

 

自定义log日志_第3张图片

你可能感兴趣的:(自定义log日志)