/
Logging.cs
45 lines (35 loc) · 1.35 KB
/
Logging.cs
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
using System;
using System.Collections.Generic;
using System.Web;
public class Logging
{
private static int LOG_IND = 1;//if you want to disable it by config, get the value from resource of config file
public static string LogFile_path = HttpContext.Current.Server.MapPath("~\\LOG\\") + DateTime.Now.ToString("yyyyMMdd") + ".txt";
public static void writeSpecLog(string ps_text, string ps_path)
{
if (LOG_IND == 1)
{
if (!System.IO.Directory.Exists(ps_path.Substring(0, ps_path.LastIndexOf("\\"))))
{
System.IO.Directory.CreateDirectory(ps_path.Substring(0, ps_path.LastIndexOf("\\")));
}
System.IO.StreamWriter file = new System.IO.StreamWriter(ps_path, true, System.Text.Encoding.UTF8);
file.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + ps_text);
file.Close();
file.Dispose(); file = null;
}
}
public static void writeLog(string ps_text)
{
writeSpecLog(ps_text, LogFile_path);
}
public static void writeLog(Exception ex)
{
if (ex.Source.ToLower() == "entityframework")
{
writeSpecLog(ex.InnerException.Message, LogFile_path);
}
writeSpecLog(ex.Message, LogFile_path);
writeSpecLog(ex.StackTrace, LogFile_path);
}
}