Skip to content

Commit

Permalink
优化请求日志记录
Browse files Browse the repository at this point in the history
优化繁体翻译
  • Loading branch information
ldqk committed Oct 6, 2022
1 parent 55e713e commit b42687d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 142 deletions.
25 changes: 22 additions & 3 deletions src/Masuit.MyBlogs.Core/Common/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static IPLocation GetIPLocation(this IPAddress ip)
parts[3] = parts[3] != "0" ? parts[3] : cityName;
return new IPLocation(parts[0], parts[2], parts[3], network?.Trim('/'), asn.AutonomousSystemNumber)
{
Address2 = countryName + cityName,
Address2 = new IPLocation.CountryCity(countryName, cityName),
Coodinate = city.Location
};
}
Expand Down Expand Up @@ -429,7 +429,7 @@ public IPLocation(string country, string province, string city, string isp, long

public string Address => new[] { Country, Province, City }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("");

public string Address2 { get; set; }
public CountryCity Address2 { get; set; } = new("", "");

public string Network => ASN.HasValue ? ISP + "(AS" + ASN + ")" : ISP;

Expand All @@ -449,7 +449,7 @@ public override string ToString()
network = "未知网络";
}

return new[] { address, Address2, network }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|");
return new[] { address, Address2?.ToString(), network }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|");
}

public static implicit operator string(IPLocation entry)
Expand All @@ -473,5 +473,24 @@ public bool Contains(params string[] s)
{
return ToString().Contains(s);
}

public record CountryCity(string Country, string City)
{
public void Deconstruct(out string country, out string city)
{
country = Country;
city = City;
}

public static implicit operator string(CountryCity entry)
{
return entry.ToString();
}

public override string ToString()
{
return Country + City;
}
}
}
}
13 changes: 8 additions & 5 deletions src/Masuit.MyBlogs.Core/Extensions/Firewall/IRequestLogger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using Masuit.MyBlogs.Core.Common;
using Masuit.MyBlogs.Core.Common;
using Masuit.MyBlogs.Core.Infrastructure;
using Masuit.MyBlogs.Core.Models.Entity;
using Masuit.Tools;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace Masuit.MyBlogs.Core.Extensions.Firewall;

Expand Down Expand Up @@ -80,9 +81,11 @@ public void Process()

while (Queue.TryDequeue(out var result))
{
var (location, network, info) = result.IP.GetIPLocation();
var location = result.IP.GetIPLocation();
result.Location = location;
result.Network = network;
result.Country = new[] { location.Country, location.Address2.Country }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|");
result.City = new[] { location.City, location.Address2.City }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|");
result.Network = location.Network;
_dataContext.Add(result);
}

Expand Down
10 changes: 6 additions & 4 deletions src/Masuit.MyBlogs.Core/Extensions/TranslateMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ private async Task Traditional(HttpContext context)
{
//设置stream存放ResponseBody
var responseOriginalBody = context.Response.Body;
var memStream = new MemoryStream();
using var memStream = new MemoryStream();
context.Response.Body = memStream;

// 执行其他中间件
await _next(context);

//处理执行其他中间件后的ResponseBody
memStream.Position = 0;
var responseReader = new StreamReader(memStream, Encoding.UTF8);
memStream.Seek(0, SeekOrigin.Begin);
using var responseReader = new StreamReader(memStream, Encoding.UTF8);
var responseBody = await responseReader.ReadToEndAsync();
memStream = new MemoryStream(Encoding.UTF8.GetBytes(ChineseConverter.Convert(responseBody, ChineseConversionDirection.SimplifiedToTraditional)));
memStream.Seek(0, SeekOrigin.Begin);
await memStream.WriteAsync(Encoding.UTF8.GetBytes(ChineseConverter.Convert(responseBody, ChineseConversionDirection.SimplifiedToTraditional)).AsMemory());
memStream.Seek(0, SeekOrigin.Begin);
await memStream.CopyToAsync(responseOriginalBody);
context.Response.Body = responseOriginalBody;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="5.1.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="6.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="CacheManager.Serialization.Json" Version="1.2.0" />
<PackageReference Include="CacheManager.StackExchange.Redis" Version="1.2.0" />
<PackageReference Include="CHTCHSConv" Version="1.0.0" />
<PackageReference Include="CLRStats" Version="1.0.0" />
<PackageReference Include="Collections.Pooled" Version="1.0.82" />
<PackageReference Include="EFCoreSecondLevelCacheInterceptor" Version="3.6.3" />
<PackageReference Include="EFCoreSecondLevelCacheInterceptor" Version="3.7.0" />
<PackageReference Include="FreeRedis" Version="1.0.2" />
<PackageReference Include="Hangfire" Version="1.7.31" />
<PackageReference Include="Hangfire.MemoryStorage" Version="1.7.0" />
Expand All @@ -60,7 +60,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.9" />
<PackageReference Include="Microsoft.Graph" Version="4.42.0" />
<PackageReference Include="Microsoft.Graph" Version="4.43.0" />
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.7" />
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
Expand Down
33 changes: 33 additions & 0 deletions src/Masuit.MyBlogs.Core/Models/Entity/RequestLogDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,57 @@ public RequestLogDetail()
[StringLength(32)]
public string Id { get; set; }

/// <summary>
/// 请求时间
/// </summary>
[Column(TypeName = "timestamp")]
public DateTime Time { get; set; }

/// <summary>
/// 用户代理
/// </summary>
[StringLength(1024), Unicode]
public string UserAgent { get; set; }

/// <summary>
/// 请求路径
/// </summary>
[StringLength(4096), Unicode]
public string RequestUrl { get; set; }

/// <summary>
/// 客户端IP
/// </summary>
[StringLength(128), Unicode]
public string IP { get; set; }

/// <summary>
/// 客户端完整地理信息
/// </summary>
[StringLength(256), Unicode]
public string Location { get; set; }

/// <summary>
/// 国家
/// </summary>
[StringLength(256), Unicode]
public string Country { get; set; }

/// <summary>
/// 城市
/// </summary>
[StringLength(256), Unicode]
public string City { get; set; }

/// <summary>
/// 运营商网络
/// </summary>
[StringLength(256)]
public string Network { get; set; }

/// <summary>
/// 跟踪id
/// </summary>
[StringLength(128)]
public string TraceId { get; set; }
}
125 changes: 0 additions & 125 deletions src/Masuit.MyBlogs.Core/MyEFCacheManagerCoreProvider.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Masuit.MyBlogs.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void BindConfig()
/// <returns></returns>
public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache(options => options.UseCustomCacheProvider<MyEFCacheManagerCoreProvider>(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).DisableLogging(true));
services.AddEFSecondLevelCache(options => options.UseCustomCacheProvider<EFCacheManagerCoreProvider>(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).DisableLogging(true).UseCacheKeyPrefix("EFCore:"));
services.AddDbContext<DataContext>((serviceProvider, opt) => opt.UseNpgsql(AppConfig.ConnString, builder => builder.EnableRetryOnFailure(10)).AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>()).EnableSensitiveDataLogging()); //配置数据库
services.AddDbContext<LoggerDbContext>(opt => opt.UseNpgsql(AppConfig.ConnString)); //配置数据库
services.ConfigureOptions();
Expand Down

0 comments on commit b42687d

Please sign in to comment.