自定义log日志

 

image

 

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);

 

image

 

问题:正由另一进程使用,因此该进程无法访问该文件

log.cs 更改

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

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


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

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

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

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

        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        * @param remark 备注
        */
        protected static void WriteLog(string type, string className, string content, string remark)
        {
            //用户浏览器标识
            //string agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] == null ? "后端调用" : HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString();

            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";//用日期对日志文件命名

            if (!File.Exists(filename))
            {
                File.Create(filename).Close();
            }

            //解决【正由另一进程使用,因此该进程无法访问该文件】
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {

                DateTime now = DateTime.Now;

                string write_content = "[" + time + "]【" + type + "" + remark + " (" + className + "): " + content;//+ "(" + agent + ")";
                byte[] bytes = null;
                if (now.Hour >= 0 && now.Hour < 8)
                {
                    //向日志文件写入内容
                    write_content = "「凌晨」" + write_content;
                    bytes = Encoding.Default.GetBytes(write_content);
                }
                else if (now.Hour >= 8 && now.Hour < 12)
                {
                    //向日志文件写入内容
                    write_content = "【上午】" + write_content;
                    bytes = Encoding.Default.GetBytes(write_content);
                }
                else if (now.Hour >= 12 && now.Hour < 18)
                {
                    //向日志文件写入内容
                    write_content = "『下午』" + write_content;
                    bytes = Encoding.Default.GetBytes(write_content);
                }
                else if (now.Hour >= 18 && now.Hour < 24)
                {
                    //向日志文件写入内容
                    write_content = "〖晚上〗" + write_content;
                    bytes = Encoding.Default.GetBytes(write_content);
                }
                //2、写操作
                fs.Position = fs.Length;
                fs.Write(bytes, 0, bytes.Length);
                //byte(13) byte(10)等效于 \r\n,直接输入\r\n不起作用
                fs.WriteByte(13);
                fs.WriteByte(10);
                fs.Flush();//清空流
            }

        }
    }
}


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

//DateTime now = DateTime.Now;

//string write_content = "[" + time + "]【" + type + "】" + remark + " (" + className + "): " + content;//+ "(" + agent + ")";

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

////关闭日志文件
//mySw.Close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值