zoukankan      html  css  js  c++  java
  • 记录操作日志

    一丶简单的记录操作日志

            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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    elasticsearch安装ik分词器
    原来你是这样的JAVA[03]-继承、多态、抽象类
    JAVA入门[23]-SpringBoot配置Swagger2
    原来你是这样的JAVA[01]-基础一瞥
    springboot + @scheduled 多任务并发
    chrome解决http自动跳转https问题
    jquery.uploadify+spring mvc实现上传图片
    JAVA POI导出excel
    使用ztree展示树形菜单结构
    shiro入门示例
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/11363567.html
Copyright © 2011-2022 走看看