zoukankan      html  css  js  c++  java
  • 自定义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)等效于 
    ,直接输入
    不起作用
                    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();
  • 相关阅读:
    感悟
    业务员如何听懂采购要吃回扣的暗语或者动作?
    回扣应该怎么给——某人的经验
    采购回扣的六种主要方式以及控制方法
    跟曹操学做事
    销售话术,一说就能“套”人,总有一款适合你
    感悟
    创业公司CEO:在公司猝死之前,我先猝死了!
    Python--实例变量
    Python--发送邮件
  • 原文地址:https://www.cnblogs.com/tangge/p/5150461.html
Copyright © 2011-2022 走看看