1 public static class WebUtil
2 {
3 /// <summary>
4 /// 写日志信息
5 /// </summary>
6 /// <param name="Message">日志信息</param>
7 public static void WriteLog(string Message, EventLogLevel logLevel = EventLogLevel.Information)
8 {
9 string sourceName = "DemoEventLog";//Application-应用程序日志
10 WriteCustomLog(sourceName, Message, logLevel);
11 }
12 /// <summary>
13 /// 写异常日志
14 /// </summary>
15 /// <param name="exp">异常信息</param>
16 public static void WriteLog(Exception exp, EventLogLevel logLevel = EventLogLevel.Error)
17 {
18 string filepath = string.Empty;
19 try
20 {
21 filepath = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
22 }
23 catch (Exception)
24 {
25
26 }
27 String Message = string.Format("
URL:
{0}
MESSAGE:
{1}
STACK TRACE:
{2}", filepath, exp.Message, exp.StackTrace);
28 string sourceName = "DemoEventLog";//Application-应用程序日志
29 WriteCustomLog(sourceName, Message, logLevel);
30 }
31 /// <summary>
32 /// 写异常日志
33 /// 设置:因为系统日志的操作是有权限控制的,所以我们还要把对系统日志操作的权限赋给asp.net用户,
34 /// 方法如下:“开始->运行”,输入命令,“regedt32”,找到“System->CurrentControlSet->Services->Eventlog”,
35 /// 选择“安全->权限->添加”,然后找到本机的“AspNet”用户,加进来并且给读取权限就好了,
36 /// 加进来后目录中会多一个“aspnet_wp account”
37 /// 系统出错后,会自动将出错信息记录到系统日志中,你可以在“开始->程序->管理工具->事件查看器”中发现一个新的项目“TownLog”,这便是记录出错信息的。
38 /// </summary>
39 /// <param name="message">日志信息</param>
40 private static void WriteCustomLog(string sourceName, string message, EventLogLevel logLevel = EventLogLevel.Information)
41 {
42 try
43 {
44 string logName = sourceName + "Log";
45 if (!(EventLog.SourceExists(sourceName)))
46 {
47 EventLog.CreateEventSource(sourceName, logName);
48 }
49
50 using (EventLog eventLog = new EventLog(logName))
51 {
52 eventLog.Source = sourceName;
53 eventLog.WriteEntry(message, (EventLogEntryType)logLevel);
54 }
55 }
56 catch (Exception ex)
57 {
58 WriteApplicationLog(ex.ToString());
59 }
60 }
61 /// <summary>
62 /// 记录应用程序日志
63 /// </summary>
64 /// <param name="message"></param>
65 public static void WriteApplicationLog(string message)
66 {
67 try
68 {
69 string sourceName = "Application";
70
71 if (!(EventLog.SourceExists(sourceName)))
72 {
73 EventLog.CreateEventSource(sourceName, sourceName);
74 }
75
76 using (EventLog eventLog = new EventLog(sourceName))
77 {
78 eventLog.Source = sourceName;
79 eventLog.WriteEntry(message, EventLogEntryType.Error);
80 }
81 }
82 catch (Exception ex)
83 {
84 throw ex;
85 }
86 }
87 /// <summary>
88 /// 获取客户端IP地址(无视代理)
89 /// </summary>
90 /// <returns>若失败则返回回送地址</returns>
91 public static string GetClientIP()
92 {
93 string userHostAddress = HttpContext.Current.Request.UserHostAddress;
94 if (string.IsNullOrEmpty(userHostAddress))
95 {
96 userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
97 }
98 //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
99 if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
100 {
101 return userHostAddress;
102 }
103 return "127.0.0.1";
104 }
105
106 /// <summary>
107 /// 检查IP地址格式
108 /// </summary>
109 /// <param name="ip"></param>
110 /// <returns></returns>
111 public static bool IsIP(string ip)
112 {
113 return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$");
114 }
115 //public static string GetClientIP(System.Web.UI.Page page)
116 //{
117 // string ipAddress = "";
118 // if (page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
119 // {
120 // ipAddress = page.Request.ServerVariables["Remote_Addr"];
121 // }
122 // else
123 // {
124 // ipAddress = page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
125 // }
126 // return ipAddress;
127 //}
128
129 //public static string GetIP(this Controller ctrl)
130 //{
131 // string ip;
132 // if (ctrl.HttpContext.Request.ServerVariables["HTTP_VIA"] != null)
133 // {
134 // ip = ctrl.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
135 // }
136 // else
137 // {
138 // ip = ctrl.HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString();
139 // }
140 // return ip;
141 //}
142 }