|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using System.IO; |
6 | | -using System.Reflection; |
7 | | -using System.Windows.Forms; |
8 | | -using System.Web; |
9 | | - |
10 | | -namespace HFSIFrameLib.COM |
11 | | -{ |
12 | | - public class Loger |
13 | | - { |
14 | | - |
15 | | - public static void WriteLog(string Message) |
16 | | - { |
17 | | - if (System.Configuration.ConfigurationManager.AppSettings["Log"] != null |
18 | | - && "TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper())) |
19 | | - { |
20 | | - string filePath = Path.Combine(Application.StartupPath, "Logs"); |
21 | | - if (HttpContext.Current != null && HttpContext.Current.Server != null && Directory.Exists(HttpContext.Current.Server.MapPath("/"))) |
22 | | - { |
23 | | - filePath = Path.Combine(HttpContext.Current.Server.MapPath("/"), "Logs"); |
24 | | - } |
25 | | - if (!Directory.Exists(filePath)) |
26 | | - { |
27 | | - Directory.CreateDirectory(filePath); |
28 | | - } |
29 | | - List<string> list = new List<string>(); |
30 | | - list.Add(string.Format("时间:{0}----------------------------------------------------------------------", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); |
31 | | - list.Add(Message); |
32 | | - |
33 | | - try |
34 | | - { |
35 | | - if (!Directory.Exists(filePath)) |
36 | | - { |
37 | | - Directory.CreateDirectory(filePath); |
38 | | - } |
39 | | - |
40 | | - File.AppendAllLines(Path.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd-HH") + ".log"), list, Encoding.Default); |
41 | | - } |
42 | | - catch |
43 | | - { } |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - public static void WriteLog(Exception ex) |
48 | | - { |
49 | | - if (System.Configuration.ConfigurationManager.AppSettings["Log"] == null |
50 | | - || !"TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper())) |
51 | | - { |
52 | | - return; |
53 | | - } |
54 | | - |
55 | | - StringBuilder sb = new StringBuilder(); |
56 | | - sb.AppendLine(string.Format("[ERRORMESSAGE]:{0}", ex.Message)); |
57 | | - sb.AppendLine(string.Format("[INNEREXCEPTION]:{0}", ex.InnerException)); |
58 | | - sb.AppendLine(string.Format("[SOURCE]:{0}", ex.Source)); |
59 | | - sb.AppendLine(string.Format("[STACKTRACE]:{0}", ex.StackTrace)); |
60 | | - WriteLog(sb.ToString()); |
61 | | - } |
62 | | - } |
63 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace DataSync |
| 10 | +{ |
| 11 | + public class Loger |
| 12 | + { |
| 13 | + |
| 14 | + public static void WriteLog(string Message) |
| 15 | + { |
| 16 | + if (System.Configuration.ConfigurationManager.AppSettings["Log"] != null |
| 17 | + && "TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper())) |
| 18 | + { |
| 19 | + string filePath = Path.Combine(Application.StartupPath, "Logs"); |
| 20 | + if (!Directory.Exists(filePath)) |
| 21 | + { |
| 22 | + Directory.CreateDirectory(filePath); |
| 23 | + } |
| 24 | + List<string> list = new List<string>(); |
| 25 | + list.Add(string.Format("时间:{0}----------------------------------------------------------------------", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); |
| 26 | + list.Add(Message); |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + if (!Directory.Exists(filePath)) |
| 31 | + { |
| 32 | + Directory.CreateDirectory(filePath); |
| 33 | + } |
| 34 | + |
| 35 | + File.AppendAllLines(Path.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd") + ".log"), list, Encoding.Default); |
| 36 | + } |
| 37 | + catch |
| 38 | + { } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static void WriteLog(Exception ex) |
| 43 | + { |
| 44 | + if (System.Configuration.ConfigurationManager.AppSettings["Log"] == null |
| 45 | + || !"TRUE".Equals(System.Configuration.ConfigurationManager.AppSettings["Log"].ToUpper())) |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + StringBuilder sb = new StringBuilder(); |
| 51 | + sb.AppendLine(string.Format("[ERRORMESSAGE]:{0}", ex.Message)); |
| 52 | + sb.AppendLine(string.Format("[INNEREXCEPTION]:{0}", ex.InnerException)); |
| 53 | + sb.AppendLine(string.Format("[SOURCE]:{0}", ex.Source)); |
| 54 | + sb.AppendLine(string.Format("[STACKTRACE]:{0}", ex.StackTrace)); |
| 55 | + WriteLog(sb.ToString()); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments