栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

关于C#.net(Winform)调试程序时的日志输出问题。

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于C#.net(Winform)调试程序时的日志输出问题。

C#.net  的日志,解决 log.cs 出现(正由另一进程使用,因此该进程无法访问该文件)问题

 

昨夜在调试程序的时候,发现log.cs 现在已经不是太好用了,于是就自己写了一个简单的日志程序。

如果你也想要自己独有的log模板,那么你可以看下我的,废话不多说,开源~

最初我也是想读取app.config取等级的, 后来想,反正是调试。就想用啥调啥就可以了。

如果能帮助到你们,还请点个赞,留个言。

后期继续更新调试方便的小工具,或者写代码心得。

上效果图~

效果图

附测试代码用例~

测试源码如下~

           // 测试日志输出
            Log.Error(this.GetType().ToString(),"Csdn,不争!写入Error内容测试");
            Log.Info(this.GetType().ToString(), "Csdn,不争!写入Info内容测试");
            Log.MostDebug(this.GetType().ToString(), "Csdn,不争!写入Most内容测试");
            for (int i = 1; i <= 10; i++)
            {
                Log.Debug(this.GetType().ToString(), "第"+i+"写入Debug内容测试");
            }

---------------------------------------------------不知所措的分割线————————————————————————

DLL源码如下~

 public class Log
    {

        //public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs";
        public static string path = System.AppDomain.CurrentDomain.BaseDirectory + "logs";

       
        public static void MostDebug(string className, string content)
        {

            WriteLog("MostDebug", className, content, "来自程序");

        }

       
        public static void Debug(string className, string content)
        {

            WriteLog("DEBUG", className, content, "来自程序");

        }

       
        public static void Info(string className, string content)
        {

            WriteLog("INFO", className, content, "来自程序");

        }

       
        public static void Error(string className, string content)
        {

            WriteLog("ERROR", className, content, "来自程序");

        }

       
        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 < 6)
                {
                    //向日志文件写入内容
                    write_content = "「凌晨」" + write_content;
                    bytes = Encoding.Default.GetBytes(write_content);
                }
                else if (now.Hour >= 6 && 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)等效于 rn,直接输入rn不起作用
                fs.WriteByte(13);
                fs.WriteByte(10);
                fs.Flush();//清空流
            }

        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910992.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号