public bool Write(LogEntity logInfo) { //自定义添加的属性 string propertiesMemberId = "MemberID"; string propertiesUserNick = "UserNick"; string propertiesServiceName = "ServiceName"; string propertiesAction = "Action"; string propertiesMethod = "Method"; string propertiesParameters = "Parameters"; string propertiesClientIpAddress = "ClientIP"; string propertiesClientName = "ClientName"; string propertiesBrowserInfo = "BrowserInfo"; string propertiesException = "Exception"; string propertiesExceptionMessage = "ExceptionMessage"; string propertiesStacktrace = "StackTrace"; string propertiesCustomData = "CustomData"; string propertiesExecutionDuration = "ExecutionDuration"; string propertiesOperatingAddress = "OperatingAddress"; string propertiesOS = "OS"; string propertiesTraceID = "TraceID"; string propertiesEventID = "EventID"; string pathlog4net = ConfigFilePath; XmlConfigurator.Configure(new FileInfo(pathlog4net)); try { GlobalContext.Properties[propertiesUserNick] = logInfo.UserNick; GlobalContext.Properties[propertiesMemberId] = logInfo.MemberID; GlobalContext.Properties[propertiesServiceName] = logInfo.ServiceName; GlobalContext.Properties[propertiesAction] = logInfo.Action; GlobalContext.Properties[propertiesMethod] = logInfo.Method; GlobalContext.Properties[propertiesParameters] = logInfo.Parameters; GlobalContext.Properties[propertiesClientIpAddress] = logInfo.ClientIP; GlobalContext.Properties[propertiesClientName] = logInfo.ClientName; GlobalContext.Properties[propertiesBrowserInfo] = logInfo.BrowserInfo; GlobalContext.Properties[propertiesCustomData] = logInfo.CustomData; GlobalContext.Properties[propertiesException] = logInfo.Exception == null ? "" : logInfo.Exception; GlobalContext.Properties[propertiesExceptionMessage] = logInfo.ExceptionMessage == null ? "" : logInfo.ExceptionMessage; GlobalContext.Properties["StackTrace"] = logInfo.StackTrace; GlobalContext.Properties[propertiesStacktrace] = logInfo.StackTrace; LogicalThreadContext.Properties[propertiesExecutionDuration] = logInfo.ExecutionDuration; LogicalThreadContext.Properties["Browser"] = $"{logInfo.BrowserInfo}-Custom value BrowserInfo"; GlobalContext.Properties[propertiesOperatingAddress] = logInfo.OperatingAddress; GlobalContext.Properties[propertiesOS] = logInfo.OS; GlobalContext.Properties[propertiesTraceID] = logInfo.TraceID; GlobalContext.Properties[propertiesEventID] = logInfo.EventID; if (string.IsNullOrWhiteSpace(logInfo.ExceptionMessage)) { LogEncapsulation.Info(logInfo); } else { LogEncapsulation.Error(logInfo); } return(true); } catch (Exception e) { string path = AppDomain.CurrentDomain.BaseDirectory; byte[] myByte = System.Text.Encoding.UTF8.GetBytes(e.Message); using (FileStream fsWrite = new FileStream(path + "/errer.txt", FileMode.Append)) { fsWrite.Write(myByte, 0, myByte.Length); } return(false); } }
public override void OnResultExecuted(ResultExecutedContext filterContext) { try { if (stop.IsRunning) { stop.Stop(); } long milliseconds = stop.ElapsedMilliseconds; //登录ID var MemberID = filterContext.HttpContext.Session["MemberID"]; var UserNick = filterContext.HttpContext.Session["UserNick"]; //请求参数post string RequestForm = filterContext.HttpContext.ApplicationInstance.Request.Form.ToString(); //请求参数 get var QueryString = filterContext.HttpContext.ApplicationInstance.Request.QueryString.ToString(); //客户端信息 var Browser = filterContext.HttpContext.Request.Browser; int?nullId = null; var LogEntitys = new LogEntity() { TraceID = ((System.Web.HttpRequestWrapper)((System.Web.Mvc.Controller)filterContext.Controller).Request).AnonymousID ?? string.Empty, EventID = ((System.Web.HttpRequestWrapper)((System.Web.Mvc.Controller)filterContext.Controller).Request).AnonymousID ?? string.Empty, Category = Convert.ToString(HttpContextCategory.Response), MemberID = MemberID == null ? nullId : Convert.ToInt32(MemberID), UserNick = Convert.ToString(UserNick) ?? HttpContextClient.Host, ExecutionDuration = milliseconds, ClientIP = HttpContextClient.IP, ClientName = HttpContextClient.Host, OperatingAddress = HttpContextClient.GetClientALLIP(), Action = $"{filterContext.RequestContext.RouteData.Values["controller"].ToString()}|{ filterContext.RequestContext.RouteData.Values["action"].ToString()}", Method = ((System.Web.HttpRequestWrapper)((System.Web.Mvc.Controller)filterContext.Controller).Request).RequestType, ServiceName = filterContext.Controller.GetType().ToString(), OS = $"{Browser.Type}/{Browser.Version}/{Browser.Platform}丨{Browser.EcmaScriptVersion.Major.ToString()}", BrowserInfo = $"{((System.Web.HttpRequestWrapper)((System.Web.Mvc.Controller)filterContext.Controller).Request).UserAgent}", ExceptionMessage = filterContext.Exception?.InnerException?.Message ?? string.Empty, Exception = filterContext.Exception?.Message ?? string.Empty, StackTrace = filterContext.Exception?.StackTrace ?? string.Empty, CustomData = HttpContextClient.UserRequest, Parameters = QueryString }; Log4NetHelper.logHelper.Write(LogEntitys); } catch (Exception ex) { string path = AppDomain.CurrentDomain.BaseDirectory; byte[] myByte = Encoding.UTF8.GetBytes(ex.Message + ex.StackTrace); using (FileStream fsWrite = new FileStream(path + "/Response-Errer.txt", FileMode.Append)) { fsWrite.Write(myByte, 0, myByte.Length); } } finally { base.OnResultExecuted(filterContext); } }