zoukankan      html  css  js  c++  java
  • c#写日志方法

    //日志内容,文件名
    private string writelog(string value,string name )
            {
                string strPath = "";
                try
                {
                    strPath = System.Web.HttpContext.Current.Server.MapPath("log");
                }
                catch { }
                if (strPath == "") strPath = Application.StartupPath;
                FileStream logFile = null;
                string strDate = DateTime.Now.Year.ToString();
                strDate += "-"+DateTime.Now.Month.ToString();
                strDate += "-" + DateTime.Now.Day.ToString();
                if (!Directory.Exists(strPath+"\log"))
                    Directory.CreateDirectory(strPath+"\log");
                if (!Directory.Exists(strPath + "\log\" + strDate))
                    Directory.CreateDirectory(strPath + "\log\" + strDate);
                strPath = strPath + "\log\" + strDate + "\" + name + ".txt";
                if (logFile == null)
                    logFile = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);     
                string strLine=Environment.NewLine;
                string strTime=DateTime.Now.ToString("HH:mm:ss");
                string str = "";
                if (name != "upbeifensql" && name != "upedsql" && name != "lianjisql")
                    str = strTime + strLine;
                str += value + strLine;
                if (name != "upbeifensql" && name != "upedsql" && name != "lianjisql")
                    str += "*****************************************************************************************************************************" + strLine;
                if (name == "upbeifensql" || name == "upedsql" || name == "lianjisql")
                {
                    CompressionHelper compress = new CompressionHelper();
                    str = compress.CompressToString(str);
                    str += strLine;
                }
                byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
                logFile.Position = logFile.Length;
                logFile.Write(bytes, 0, (int)bytes.Length);
                logFile.Close();
                logFile = null;
                return strPath;
            }
  • 相关阅读:
    迭代器
    装饰器
    函数对象和闭包
    函数的使用
    文件操作
    基本数据类型及内置方法
    MySQL数据库
    网络编程进阶(进程、线程、协程、IO模型)
    网络编程基础---网络通讯原理、ssh远程执行命令、粘包问题处理、文件传输处理
    面向对象、类、元类、封装、异常处理
  • 原文地址:https://www.cnblogs.com/valiant1882331/p/4113004.html
Copyright © 2011-2022 走看看