From 14e7f75512a5e3a5288bcf679c21107e38a32951 Mon Sep 17 00:00:00 2001
From: Hinse <756681202@qq.com>
Date: Fri, 30 Jul 2021 10:02:44 +0800
Subject: [PATCH] =?UTF-8?q?1.NET=E5=9F=BA=E7=A1=80=E5=BA=93=E5=8D=87?=
=?UTF-8?q?=E7=BA=A7=E4=B8=BA5.0.301=202.Furion=E5=BA=93=E5=8D=87=E7=BA=A7?=
=?UTF-8?q?=203.=E5=A2=9E=E5=8A=A0serilog=20seq=E6=97=A5=E5=BF=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ResultInfos/ResultService.cs | 1 -
Waste.Core/LoggerHelper.cs | 99 +++++++++++++++++++
Waste.Core/Waste.Core.csproj | 10 +-
.../Handlers/RESTfulResultProvider.cs | 39 +++++---
Waste.Web.Core/Startup.cs | 5 +-
Waste.Web.Core/Waste.Web.Core.csproj | 1 +
Waste.Web.Core/Waste.Web.Core.xml | 16 +--
Waste.Web.Entry/Program.cs | 1 +
.../waste.ybhdmob.com.pubxml.user | 2 +-
9 files changed, 146 insertions(+), 28 deletions(-)
create mode 100644 Waste.Core/LoggerHelper.cs
diff --git a/Waste.Application/ResultInfos/ResultService.cs b/Waste.Application/ResultInfos/ResultService.cs
index 5ff1860..0775877 100644
--- a/Waste.Application/ResultInfos/ResultService.cs
+++ b/Waste.Application/ResultInfos/ResultService.cs
@@ -180,7 +180,6 @@ namespace Waste.Application
}
var typeHex = Convert.ToHexString(typeBytes);
myPackage.Area = Convert.ToInt32(typeHex, 16).ToString();
- //十六进制转10进制
}
//000F000002.16进制
var areaHex = Convert.ToHexString(areaBytes);
diff --git a/Waste.Core/LoggerHelper.cs b/Waste.Core/LoggerHelper.cs
new file mode 100644
index 0000000..de8c5bf
--- /dev/null
+++ b/Waste.Core/LoggerHelper.cs
@@ -0,0 +1,99 @@
+using Microsoft.AspNetCore.Http;
+using Serilog;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Waste.Core
+{
+ public static class LoggerHelper
+ {
+ public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
+ {
+ var request = httpContext.Request;
+
+ // Set all the common properties available for every request
+ diagnosticContext.Set("Host", request.Host);
+ diagnosticContext.Set("Scheme", request.Scheme);
+
+ // Only set it if available. You're not sending sensitive data in a querystring right?!
+ if (request.QueryString.HasValue)
+ {
+ diagnosticContext.Set("QueryString", request.QueryString.Value);
+ }
+ string ip = string.Empty;
+ if (request.Headers.ContainsKey("X-Real-IP"))
+ {
+ ip = request.Headers["X-Real-IP"].ToString();
+ }
+ else
+ {
+ ip = httpContext.Connection.RemoteIpAddress.ToString();
+ }
+ if (!string.IsNullOrEmpty(ip))
+ {
+ diagnosticContext.Set("IP", ip);
+ }
+ if (!string.IsNullOrEmpty(request.Headers["token"].ToString()))
+ {
+ diagnosticContext.Set("token", request.Headers["token"].ToString());
+ }
+ if (!string.IsNullOrEmpty(request.Headers["Authorization"].ToString()))
+ {
+ diagnosticContext.Set("Authorization", request.Headers["Authorization"].ToString());
+ }
+ if (!string.IsNullOrEmpty(request.Headers["X-Authorization"].ToString()))
+ {
+ diagnosticContext.Set("X-Authorization", request.Headers["X-Authorization"].ToString());
+ }
+ if (!string.IsNullOrEmpty(request.Headers["User-Agent"].ToString()))
+ {
+ diagnosticContext.Set("User-Agent", request.Headers["User-Agent"].ToString());
+ }
+ if (request.Method == "POST" && request.ContentLength > 0)
+ {
+ if (httpContext.RequestAborted.IsCancellationRequested)
+ {
+ diagnosticContext.Set("postdata", "用户取消了操作");
+ }
+ else
+ {
+ string postdata = string.Empty;
+ try
+ {
+ if (request.Body.Length > 0)
+ {
+ request.EnableBuffering();
+ request.Body.Position = 0;
+ using (var sr = new StreamReader(request.Body))
+ {
+ postdata = Task.Factory.StartNew(sr.ReadToEndAsync, TaskCreationOptions.LongRunning)
+ .GetAwaiter().GetResult().Result;
+ // postdata = await sr.ReadToEndAsync();
+ }
+ if (!string.IsNullOrEmpty(postdata))
+ {
+ diagnosticContext.Set("PostData", postdata);
+ }
+ }
+ }
+ catch (Exception)
+ {
+ }
+ }
+ }
+ // Set the content-type of the Response at this point
+ diagnosticContext.Set("ContentType", httpContext.Response.ContentType);
+
+ // Retrieve the IEndpointFeature selected for the request
+ var endpoint = httpContext.GetEndpoint();
+ if (endpoint is object) // endpoint != null
+ {
+ diagnosticContext.Set("EndpointName", endpoint.DisplayName);
+ }
+ }
+ }
+}
diff --git a/Waste.Core/Waste.Core.csproj b/Waste.Core/Waste.Core.csproj
index 478d121..836b069 100644
--- a/Waste.Core/Waste.Core.csproj
+++ b/Waste.Core/Waste.Core.csproj
@@ -15,11 +15,11 @@
-
-
-
-
-
+
+
+
+
+
diff --git a/Waste.Web.Core/Handlers/RESTfulResultProvider.cs b/Waste.Web.Core/Handlers/RESTfulResultProvider.cs
index b5986f2..af4d650 100644
--- a/Waste.Web.Core/Handlers/RESTfulResultProvider.cs
+++ b/Waste.Web.Core/Handlers/RESTfulResultProvider.cs
@@ -1,6 +1,7 @@
using Furion.DataValidation;
using Furion.DependencyInjection;
using Furion.UnifyResult;
+using Furion.UnifyResult.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
@@ -16,18 +17,22 @@ namespace Waste.Web.Core
///
/// RESTFUL 风格返回值
///
- [SkipScan, UnifyModel(typeof(RESTfulResult<>))]
+ [SuppressSniffer, UnifyModel(typeof(RESTfulResult<>))]
public class RESTfulResultProvider : IUnifyResultProvider
{
+
///
/// 异常返回值
///
///
+ ///
///
- public IActionResult OnException(ExceptionContext context)
+ public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
{
//解析异常信息
- var (StatusCode, ErrorCode, Errors) = UnifyContext.GetExceptionMetadata(context);
+ var ErrorCode = metadata.ErrorCode;
+ var StatusCode = metadata.StatusCode;
+ var Errors = metadata.Errors;
object data;
// 处理内容结果
if (context.Result is ContentResult contentResult) data = contentResult.Content;
@@ -50,10 +55,12 @@ namespace Waste.Web.Core
///
///
///
- ///
+ ///
///
- public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultStatusCodesOptions options = null)
+ public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings)
{
+ // 设置响应状态码
+ UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
switch (statusCode)
{
// 处理 401 状态码
@@ -90,10 +97,10 @@ namespace Waste.Web.Core
/// 成功返回值
///
///
+ ///
///
- public IActionResult OnSucceeded(ActionExecutedContext context)
+ public IActionResult OnSucceeded(ActionExecutedContext context, object data)
{
- object data;
// 处理内容结果
if (context.Result is ContentResult contentResult) data = contentResult.Content;
// 处理对象结果
@@ -115,18 +122,26 @@ namespace Waste.Web.Core
/// 验证失败返回值
///
///
- ///
- ///
- ///
+ ///
///
- public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, IEnumerable validationResults, string validateFailedMessage)
+ public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
{
+ var msg = "发生错误";
+ var validationResults = metadata.ValidationResult;
+ if (validationResults != null && validationResults.Count() > 0)
+ {
+ var messages = validationResults.FirstOrDefault().Value;
+ if (messages.Length > 0)
+ {
+ msg = messages[0];
+ }
+ }
return new JsonResult(new RESTfulResult