• 记录操作日志


    一丶简单的记录操作日志

            public void WriteLog(string msg)
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("消息:" + msg);
                        sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sw.WriteLine("**************************************************");
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("异常:" + e.Message);
                        sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
                        sw.WriteLine("**************************************************");
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }

     二、

           /// <summary>
            /// 写文件
            /// </summary>
            /// <param name="str"></param>
            static void WriteLog(string str)
            {
                if (!Directory.Exists("ErrLog"))
                {
                    Directory.CreateDirectory("ErrLog");
                }
    
                string logPath ="ErrLog\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
    
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine(str);
                    sw.WriteLine("---------------------------------------------------------");
                    sw.Close();
                }
            }
    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    layui下拉框数据过万渲染渲染问题解决方案
    eclipse debug启动时tomcat报错
    用eclipse的同一个tomcat启动两个javaweb项目
    Json
    @ResponseBody
    SpringMVC Controller 介绍
    tianmao项目的学习笔记
    Thymeleaf入门
    Thymeleaf th:include,th:replace使用
    X 在Windows上使用orakill结束oracle会话的线程
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/11363567.html
Copyright © 2020-2023  润新知