duhe
6 天以前 315b12766c0ea48059c79c48bc57f0d40c65438d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
 
namespace ViewAPI
{
    public class Log
    {
        public static void WriteListToTextFile(List<string> list)
        {
            string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\";
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "_sql.log";
            string txtFile = uploadPath + fileName;
            //创建一个文件流,用以写入或者创建一个StreamWriter 
            FileStream fs = new FileStream(txtFile, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Flush();
            // 使用StreamWriter来往文件中写入内容 
            sw.BaseStream.Seek(0, SeekOrigin.Begin);
            for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]);
            //关闭此文件t 
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        public static void LogInfo(string msg)
        {
            string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\";
 
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string fileName = DateTime.Now.Ticks.ToString() + ".log";
 
            FileStream fs;
            StreamWriter sw;
            string sFileName = uploadPath + fileName;
            if (File.Exists(sFileName))
            //验证文件是否存在,有则追加,无则创建
            {
                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
            }
            sw = new StreamWriter(fs);
            msg = String.Format("------------------------{0}------------------------------\r\n", DateTime.Now) + msg;
            msg += "\r\n";
            sw.WriteLine(msg);
            sw.Close();
            fs.Close();
        }
        public static void LogSaveInfo(string msg)
        {
            string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\";
 
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
 
            FileStream fs;
            StreamWriter sw;
            string sFileName = uploadPath + fileName;
            if (File.Exists(sFileName))
            //验证文件是否存在,有则追加,无则创建
            {
                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
            }
            sw = new StreamWriter(fs);
            msg = String.Format("------------------------{0}------------------------------\r\n", DateTime.Now) + msg;
            msg += "\r\n";
            sw.WriteLine(msg);
            sw.Close();
            fs.Close();
        }
    }
}