时间: 2020-11-22|51次围观|0 条评论

//日志内容,文件名
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;
        }

 

转载于:https://www.cnblogs.com/valiant1882331/p/4113004.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/96835517

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《c#写日志方法
   

还没有人抢沙发呢~