VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#日志类

 
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
92
public static class LogHelper
    {
        private static readonly Queue<Dictionary<stringstring>> Que = new Queue<Dictionary<stringstring>>();
        private static readonly Object Object = new Object();
        private static EnumLogLevel LogType { getset; }
 
        static LogHelper()
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                while (true)
                {
                    if (Que.Count <= 0)
                    {
                        Thread.Sleep(5000);
                        continue;
                    }
                    lock (Object)
                    {
                        if (Que.Count <= 0)
                        {
                            Thread.Sleep(5000);
                            continue;
                        }
                        var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
                        var path = string.Format("{0}Logs\\", currentDirectory);
                        if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }
                        var fileName = Path.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
                        using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            using (var sw = new StreamWriter(fs))
                            {
                                sw.BaseStream.Seek(0, SeekOrigin.End);
                                var col = Que.Dequeue();
                                foreach (KeyValuePair<stringstring> m in col)
                                {
                                    sw.WriteLine(m.Key + ":{0}", m.Value);
                                }
                                sw.Flush(); sw.Dispose(); sw.Close();
                            }
                        }
                    }
                }
            });
        }
        public static string WriteLog(this string str, EnumLogLevel type = EnumLogLevel.Info)
        {
            var logLevel = ConfigurationManager.AppSettings["LogLevel"];
            var level = string.IsNullOrWhiteSpace(logLevel) ? EnumLogLevel.Info.ToString() : logLevel;
 
            if ((int)type < (int)Enum.Parse(typeof(EnumLogLevel), level))
            {
                return string.Empty;
            }
            lock (Object)
            {
                LogType = type;
                var st = new StackTrace(new StackFrame(1, true));
                StackFrame sf = st.GetFrame(0);
                var dic = new Dictionary<stringstring>
                {
                    {"发生时间", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ffff dddd")},
                    {"日志级别", LogType.ToString()},
                    {"日志消息", str},
                    {"当前方法", sf.GetMethod().Name},
                    {"发生位置", st.ToString()}
                };
                Que.Enqueue(dic);
            }
            return str;
        }
    }
 
    /// <summary>
    /// All: 表示最低的日志等级。
    /// Debug: 这个等级表示用于调试程序的正常的事件信息;
    /// Info: 默认的等级
    /// Warn: 表示可能对系统有损害的情况;
    /// Error: 表示较严重的错误等级,但是程序可以继续运行的信息;
    /// Fatal: 表示非常严重的错误等级,记录极有可能导致应用程序终止运行的致命错误信息;
    /// Off: 表示最高的等级,如果一个logger的等级标记为Off, 将不会记录任何信息;
    /// </summary>
    public enum EnumLogLevel
    {
        All,
        Debug,
        Info,
        Warn,
        Error,
        Fatal,
        Off
    }


相关教程