王 垚
2021-11-12 0ce538e3c3dc01153ce1bfc2d75276881206c222
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
 
namespace ZD.Cloud.Logger
{
    public class LogHelper
    {
        private readonly static object locker = new object();
        public static void Info(string msg, bool isAsync = true)
        {
            Func(LogType.Info, msg);
        }
 
        public static void Error(string msg, bool isAsync = true)
        {
            Func(LogType.Error, msg);
        }
 
        public static void Error(Exception ex, bool isAsync = true)
        {
            Func(LogType.Error, "", ex);
        }
 
        public static void Warn(string msg, bool isAsync = true)
        {
            Func(LogType.Warn, msg);
        }
        public static void Warn(Exception ex, bool isAsync = true)
        {
            Func(LogType.Warn, "", ex);
        }
 
        //带文件夹名称
        public static void Error(string logFol, string msg, bool isAsync = true)
        {
            FuncWithFol(LogType.Error, logFol, msg);
        }
        public static void Error(string logFol, Exception ex, bool isAsync = true)
        {
            FuncWithFol(LogType.Error, logFol, "", ex);
        }
        public static void Warn(string logFol, string msg, bool isAsync = true)
        {
            FuncWithFol(LogType.Warn, logFol, msg);
        }
 
        public static void Warn(string logFol, Exception ex, bool isAsync = true)
        {
            FuncWithFol(LogType.Warn, logFol, "", ex);
        }
 
        public static void Info(string logFol, string msg, bool isAsync = true)
        {
            FuncWithFol(LogType.Info, logFol, msg);
        }
 
        private static void Func(LogType type, string msg = "", Exception ex = null, bool isAsync = true)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string dt = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss fff");
                sb.AppendFormat("{0}  {1}  -  {2}", dt, type, msg);
                if (type == LogType.Error)
                {
                    sb.AppendFormat("  [Exception:{0}]", ex);
                }
                WriteLog("", sb.ToString());
 
                if (type == LogType.Warn)
                {
                    Mail.SendMail(sb.ToString());
                }
            }
            finally
            {
            }
        }
 
        private static void FuncWithFol(LogType type, string logFol, string msg = "", Exception ex = null, bool isAsync = true)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string dt = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss fff");
                sb.AppendFormat("{0}  {1}  -  {2}", dt, type, msg);
                if (type == LogType.Error)
                {
                    sb.AppendFormat("  [Exception:{0}]", ex);
                }
                WriteLog(logFol, sb.ToString());
 
                if (type == LogType.Warn)
                {
                    Mail.SendMail(sb.ToString());
                }
            }
            finally
            {
            }
        }
 
 
        private static void WriteLog(string logFol, string logContent)
        {
            if (string.IsNullOrWhiteSpace(logContent)) return;
 
            if (string.IsNullOrWhiteSpace(logFol))
            {
                logFol = "zdlog";
            }
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(2);//1代表上级,2代表上上级,以此类推
            MethodBase method = frame.GetMethod();
            var className = method.ReflectedType.Name;
            string logDir = AppDomain.CurrentDomain.BaseDirectory;
            var logPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
            if (string.IsNullOrEmpty(logPath))
            {
                logPath = string.Format(@"{0}\{1}\{2}", logDir, logFol, className);
            }
 
            logPath = logPath.Replace("_", "").Replace(" ", "").Replace("/r", "").Replace("/n", "");
 
            if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
 
            string filename = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString(@"yyyyMMdd"));
            if (File.Exists(filename))
            {
                FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(logContent);
                sr.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(logContent);
                sr.Close();
                fs.Close();
            }
 
        }
 
        public enum LogType
        {
            Info,
            Error,
            Warn
        }
    }
}