码迷,mamicode.com
首页 > Windows程序 > 详细

c#写日志方法

时间:2014-11-21 16:02:34      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   文件   

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

 

c#写日志方法

标签:style   blog   http   io   ar   color   os   sp   文件   

原文地址:http://www.cnblogs.com/valiant1882331/p/4113004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!