zhaojs
2023-09-27 74098f1401afe40f961d1d167bb18dd0a71c4d59
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Web;
using System.IO;
 
/// <summary>
/// 文本日志记录辅助类
/// </summary>
public class LogTextHelper
{
    static string LogFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
    public static bool RecordLog = true;
    public static bool DebugLog = false;
    static LogTextHelper()
    {
        if (!Directory.Exists(LogFolder))
        {
            Directory.CreateDirectory(LogFolder);
        }
    }
 
    /// <summary>
    /// 记录文本日志
    /// </summary>
    /// <param name="message">日志信息</param>
    public static void WriteLine(string message)
    {
        string temp = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]    ") + message + "\r\n\r\n";
        string fileName = DateTime.Now.ToString("yyyyMMdd") + ".log";
        try
        {
            if (RecordLog)
            {
                File.AppendAllText(Path.Combine(LogFolder, fileName), temp, Encoding.GetEncoding("GB2312"));
            }
            if (DebugLog)
            {
                Console.WriteLine(temp);
            }
        }
        catch
        {
        }
    }
 
    /// <summary>
    /// 记录文本日志
    /// </summary>
    /// <param name="message">日志信息</param>
    public static void WriteLine(string fileName, string message)
    {
        string temp = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]    ") + message + "\r\n\r\n";
        //string fileName = DateTime.Now.ToString("yyyyMMdd") + ".log";
        try
        {
            if (RecordLog)
            {
                File.AppendAllText(Path.Combine(LogFolder, fileName), temp, Encoding.GetEncoding("GB2312"));
            }
            if (DebugLog)
            {
                Console.WriteLine(temp);
            }
        }
        catch
        {
        }
    }
 
    /// <summary>
    /// 记录文本日志
    /// </summary>
    /// <param name="className">类名</param>
    /// <param name="funName">方法名</param>
    /// <param name="message">日志信息</param>
    public static void WriteLine(string className, string funName, string message)
    {
        WriteLine(string.Format("{0}:{1}\r\n{2}", className, funName, message));
    }
}