using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; namespace CP.Common { public class LogHelper { //log4net日志专用 public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); public static void SetConfig() { log4net.Config.XmlConfigurator.Configure(); } public static void SetConfig(FileInfo configFile) { log4net.Config.XmlConfigurator.Configure(configFile); } /// /// 普通的文件记录日志 /// /// public static void WriteLog(string info) { if (loginfo.IsInfoEnabled) { loginfo.Info(info); } } /// /// 错误日志 /// /// /// public static void WriteLog(string info, Exception se) { if (logerror.IsErrorEnabled) { logerror.Error(info, se); } } } /// /// 系统错误信息 /// public class ErrorMessage { public ErrorMessage() { } public ErrorMessage(Exception ex, string type) { MsgType = ex.GetType().Name; Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message; StackTrace = ex.StackTrace; Source = ex.Source; Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Assembly = ex.TargetSite.Module.Assembly.FullName; Method = ex.TargetSite.Name; Type = type; DotNetVersion = Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision; DotNetBit = (Environment.Is64BitProcess ? "64" : "32") + "位"; OSVersion = Environment.OSVersion.ToString(); CPUCount = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); CPUType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); OSBit = (Environment.Is64BitOperatingSystem ? "64" : "32") + "位"; var request = HttpContext.Current.Request; IP = GetIpAddr(request) + ":" + request.Url.Port; IISVersion = request.ServerVariables["SERVER_SOFTWARE"]; UserAgent = request.UserAgent; Path = request.Path; HttpMethod = request.HttpMethod; } /// /// 消息类型 /// public string MsgType { get; set; } /// /// 消息内容 /// public string Message { get; set; } /// /// 请求路径 /// public string Path { get; set; } /// /// 程序集名称 /// public string Assembly { get; set; } /// /// 异常参数 /// public string ActionArguments { get; set; } /// /// 请求类型 /// public string HttpMethod { get; set; } /// /// 异常堆栈 /// public string StackTrace { get; set; } /// /// 异常源 /// public string Source { get; set; } /// /// 服务器IP 端口 /// public string IP { get; set; } /// /// 客户端浏览器标识 /// public string UserAgent { get; set; } /// /// .NET解释引擎版本 /// public string DotNetVersion { get; set; } /// /// 应用程序池位数 /// public string DotNetBit { get; set; } /// /// 操作系统类型 /// public string OSVersion { get; set; } /// /// 操作系统位数 /// public string OSBit { get; set; } /// /// CPU个数 /// public string CPUCount { get; set; } /// /// CPU类型 /// public string CPUType { get; set; } /// /// IIS版本 /// public string IISVersion { get; set; } /// /// 请求地址类型 /// public string Type { get; set; } /// /// 是否显示异常界面 /// public bool ShowException { get; set; } /// /// 异常发生时间 /// public string Time { get; set; } /// /// 异常发生方法 /// public string Method { get; set; } //这段代码用户请求真实IP private static string GetIpAddr(HttpRequest request) { //HTTP_X_FORWARDED_FOR string ipAddress = request.ServerVariables["x-forwarded-for"]; if (!IsEffectiveIP(ipAddress)) { ipAddress = request.ServerVariables["Proxy-Client-IP"]; } if (!IsEffectiveIP(ipAddress)) { ipAddress = request.ServerVariables["WL-Proxy-Client-IP"]; } if (!IsEffectiveIP(ipAddress)) { ipAddress = request.ServerVariables["Remote_Addr"]; if (ipAddress.Equals("127.0.0.1") || ipAddress.Equals("::1")) { // 根据网卡取本机配置的IP IPAddress[] AddressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; foreach (IPAddress _IPAddress in AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { ipAddress = _IPAddress.ToString(); break; } } } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.Length > 15) { if (ipAddress.IndexOf(",") > 0) { ipAddress = ipAddress.Substring(0, ipAddress.IndexOf(",")); } } return ipAddress; } /// /// 是否有效IP地址 /// /// IP地址 /// bool private static bool IsEffectiveIP(string ipAddress) { return !(string.IsNullOrEmpty(ipAddress) || "unknown".Equals(ipAddress, StringComparison.OrdinalIgnoreCase)); } } }