完善日志中间件
This commit is contained in:
		
							parent
							
								
									defe09213d
								
							
						
					
					
						commit
						ed4978a6f6
					
				@ -1,5 +1,6 @@
 | 
				
			|||||||
using Microsoft.AspNetCore.Http;
 | 
					using Microsoft.AspNetCore.Http;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
using System.Diagnostics;
 | 
					using System.Diagnostics;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -45,13 +46,14 @@ namespace FastGithub.ReverseProxy
 | 
				
			|||||||
            var response = context.Response;
 | 
					            var response = context.Response;
 | 
				
			||||||
            var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
 | 
					            var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (500 <= response.StatusCode && response.StatusCode <= 599)
 | 
					            var exception = context.GetForwarderErrorFeature()?.Exception;
 | 
				
			||||||
 | 
					            if (exception == null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                this.logger.LogError(message);
 | 
					                this.logger.LogInformation(message);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                this.logger.LogInformation(message);
 | 
					                this.logger.LogError($"{message}{Environment.NewLine}{exception.Message}");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -60,7 +60,7 @@ namespace FastGithub.ReverseProxy
 | 
				
			|||||||
                using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniPattern);
 | 
					                using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniPattern);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
 | 
					                var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
 | 
				
			||||||
                await ResponseErrorAsync(context, error);
 | 
					                await HandleErrorAsync(context, error);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -85,12 +85,12 @@ namespace FastGithub.ReverseProxy
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 写入错误信息
 | 
					        /// 处理错误信息
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
        /// <param name="context"></param>
 | 
					        /// <param name="context"></param>
 | 
				
			||||||
        /// <param name="error"></param>
 | 
					        /// <param name="error"></param>
 | 
				
			||||||
        /// <returns></returns>
 | 
					        /// <returns></returns>
 | 
				
			||||||
        private static async Task ResponseErrorAsync(HttpContext context, ForwarderError error)
 | 
					        private static async Task HandleErrorAsync(HttpContext context, ForwarderError error)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (error == ForwarderError.None)
 | 
					            if (error == ForwarderError.None)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user