/// <summary> /// /// </summary> /// <param name="invocation"></param> public async void Intercept(IInvocation invocation) { Stopwatch sp = new Stopwatch(); sp.Start(); string jsonStr = JsonConvert.SerializeObject(invocation.Arguments); //方法执行前 string msg = $"当前请求方法({invocation.Method.Name});请求参数({jsonStr});请求开始..."; _ulog.Info(msg); //执行方法 invocation.Proceed(); //方法执行后 jsonStr = "获取执行后的方法异常"; try { //判断是否是异步的方法,如果是异步的方法直接返回 if (IsAsyncMethod(invocation.Method)) { return; } sp.Stop(); jsonStr = JsonConvert.SerializeObject(invocation.ReturnValue); } catch (Exception ex) { _ulog.Warn($"日志拦截器AOP异常:{ex.Message}"); } msg += $"---请求结束,返回数据{jsonStr};总耗时{sp.ElapsedMilliseconds}"; _ulog.Info(msg); }
/// <summary> /// 发送消息服务 /// </summary> /// <param name="method">请求方法Get,Post...</param> /// <param name="requestUri">请求地址</param> /// <param name="content">请求内容</param> /// <param name="headerAction">头部处理委托</param> /// <returns></returns> protected async Task <string> SendMessage(HttpMethod method, string requestUri, StringContent content) { string guidStr = Guid.NewGuid().ToString(); _uLog.Info($"{guidStr}:请求地址:{requestUri};请求方式:{method.ToString()};请求参数:{content.ReadAsStringAsync()?.Result}"); HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestUri); try { if (content != null) { requestMessage.Content = content; } HttpClient client = _clientFactory.CreateClient(); //请求超时30秒 client.Timeout = new TimeSpan(0, 0, 30); var response = await client.SendAsync(requestMessage); string reqStr = string.Empty; if (response.IsSuccessStatusCode) { reqStr = response.Content.ReadAsStringAsync().Result; } else { throw new HttpRequestConnectionException($"{requestUri}:请求失败!--返回状态异常!"); } _uLog.Info($"{guidStr}:请求地址:{requestUri},请求成功! 返回状态:{response.StatusCode},返回数据:{reqStr}"); return(reqStr); } catch (Exception ex) { _uLog.Info($"{guidStr}:请求地址:{requestUri},请求失败!"); throw new HttpRequestConnectionException($"{requestUri}:请求失败!--捕获到异常的信息【{ex.Message}】", ex); } }
public void Index() { var msg = _restful.GetMessage("Search", "sh600789"); _logger.Info(msg.Result); }