博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义log日志
阅读量:5156 次
发布时间:2019-06-13

本文共 8939 字,大约阅读时间需要 29 分钟。

 

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];        }        ///         /// 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息         ///         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();

转载于:https://www.cnblogs.com/tangge/p/5150461.html

你可能感兴趣的文章
Tomcat启动报错:java.lang.NoClassDefFoundError: javax/servlet/GenericFilter
查看>>
SVN使用教程总结[转]
查看>>
t-sne
查看>>
不同组织物料类别差异列表
查看>>
01_正则表达式_05_小结和几个案例
查看>>
web网页上面调用qq
查看>>
C语言中的多字节字符与宽字符
查看>>
入门Webpack
查看>>
【c语言】【每周一练】排序算法之归并排序
查看>>
sql server 碎片整理——DBCC SHOWCONTIG
查看>>
vue 路由的使用
查看>>
了解GO
查看>>
[转载]WSDL 教程
查看>>
小程序:兼容
查看>>
杂项-Server:Serv-U
查看>>
RDMA相关的技术网站
查看>>
关于我
查看>>
Buffer.from(str[, encoding])
查看>>
GitHub 的简单使用
查看>>
【转】MySQL常用命令总结
查看>>