zoukankan      html  css  js  c++  java
  • 写日志文件

            #region<写日志文件>
            /// <summary>
            /// 写日志文件,保存到指定的文件
            /// </summary>
            /// <param name="log">日志内容</param>
            /// <param name="fileName">保存到文件</param>
            public static void WriterLog(string log, string fileName)
            {
                try
                {
                    // System.Windows.Forms.MessageBox.Show(log);
                    string logPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Log" + fileName;
                    if (System.IO.File.Exists(logPath))
                        CheckLogFile(logPath);
                    Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + @"Log");
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(logPath, true);
                    sw.WriteLine(DateTime.Now.ToString() + "   日志: " + log);
                    sw.Flush();
                    sw.Close();
                }
                catch
                {
                }
            }
    
            /// <summary>
            /// 读取模板文件返回文件内容
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public static string ReadTemplateFile(string fileName)
            {
                try
                {
                    string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"Template" + fileName;
                    System.IO.StreamReader sr = new System.IO.StreamReader(filePath, Encoding.GetEncoding("gbk"));
                    string str = sr.ReadToEnd();
                    sr.Close();
                    return str;
                }
                catch
                {
                    return "";
                }
            }
    
            /// <summary>de 
            /// 如果日志文件大于1M则备份,并清空文件
            /// </summary>
            /// <param name="filePath"></param>
            private static void CheckLogFile(string filePath)
            {
                try
                {
                    string path = Path.GetDirectoryName(filePath) + @"aklog";
                    System.IO.FileInfo info = new FileInfo(filePath);
                    if (info.Length > 1048576)
                    {
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        info.CopyTo(path + @"" + DateTime.Now.Ticks.ToString() + ".txt");
                        info.Delete();
                    }
                }
                catch (Exception ex)
                {
                    WriterLog(ex.Message);
                }
            }
    
            /// <summary>
            /// 写日志文件,保存到默认的文件:Bpfaq.log
            /// </summary>
            /// <param name="log">日志内容</param>
            public static void WriterLog(string log)
            {
                WriterLog(log, "ERP.log");
            }
            #endregion<结束日志文件>
  • 相关阅读:
    Python--文件操作
    Python--数据类型整理
    u-boot之NAND启动与NOR启动的区别
    u-boot之make all执行过程分析
    编译过程和符号表重定位问题、静态和动态链接
    u-boot之make <board_name>_config执行过程分析
    u-boot之ARM920T的start.S分析
    在使用Myeclipse时,用Tomcat添加部署项目的时候报错,或启动tomcat报错
    关于JDK,tomcat,eclipse的配置
    我的博客
  • 原文地址:https://www.cnblogs.com/lusunqing/p/3150767.html
Copyright © 2011-2022 走看看