Skip to content

Commit a5666dc

Browse files
committed
fix: 更新Swashubuckle版本,同时优化FreeSQL工作单元应用
1 parent 30651f1 commit a5666dc

File tree

11 files changed

+163
-77
lines changed

11 files changed

+163
-77
lines changed

components/src/Bing.Biz.OAuthLogin/Bing.Biz.OAuthLogin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bing是一个.net core平台下的应用框架,旨在于提升小型团队的
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Bing.Utils.Http" Version="1.2.2" />
10+
<PackageReference Include="Bing.Utils.Http" Version="1.4.0-preview-20220831-2" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

framework/src/Bing.FreeSQL.MySql/Bing/FreeSQL/Extensions.Services.cs

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,85 @@ public static partial class Extensions
2121
/// <summary>
2222
/// 注册MySql工作单元服务
2323
/// </summary>
24-
/// <typeparam name="TUnitOfWOrk">工作单元接口类型</typeparam>
24+
/// <typeparam name="TUnitOfWork">工作单元接口类型</typeparam>
2525
/// <typeparam name="TUnitOfWorkImplementation">工作单元实现类型</typeparam>
2626
/// <param name="services">服务集合</param>
2727
/// <param name="connection">连接字符串</param>
2828
/// <param name="setupAction">配置操作</param>
2929
/// <param name="freeSqlSetupAction">FreeSql配置操作</param>
30-
public static IServiceCollection AddMySqlUnitOfWork<TUnitOfWOrk, TUnitOfWorkImplementation>(this IServiceCollection services
30+
public static IServiceCollection AddMySqlUnitOfWork<TUnitOfWork, TUnitOfWorkImplementation>(this IServiceCollection services
3131
, string connection
32-
, Action<FreeSqlBuilder> setupAction = null
33-
, Action<IFreeSql> freeSqlSetupAction = null)
34-
where TUnitOfWOrk : class, IUnitOfWork
35-
where TUnitOfWorkImplementation : UnitOfWorkBase, TUnitOfWOrk
32+
, Action<IServiceProvider, FreeSqlBuilder> setupAction = null
33+
, Action<IServiceProvider, IFreeSql> freeSqlSetupAction = null)
34+
where TUnitOfWork : class, IUnitOfWork
35+
where TUnitOfWorkImplementation : UnitOfWorkBase, TUnitOfWork
3636
{
37-
38-
var freeSqlBuilder = new FreeSqlBuilder()
37+
Func<IServiceProvider, FreeSqlWrapper> freeSqlWrapper = s =>
38+
{
39+
var freeSqlBuilder = new FreeSqlBuilder()
3940
.UseConnectionString(DataType.MySql, connection)
4041
.UseLazyLoading(false);
41-
setupAction?.Invoke(freeSqlBuilder);
42+
setupAction?.Invoke(s, freeSqlBuilder);
4243

43-
var freeSql = freeSqlBuilder.Build();
44-
freeSqlSetupAction?.Invoke(freeSql);
45-
freeSql.Aop.AuditValue += (s, e) =>
46-
{
47-
// 乐观锁
48-
if (e.AuditValueType == AuditValueType.Insert || e.AuditValueType == AuditValueType.Update)
49-
{
50-
if (e.Property.Name == AuditedPropertyConst.Version)
51-
if (e.Value is byte[] bytes && bytes.Length == 0)
52-
e.Value = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
53-
}
54-
// 时间
55-
if (e.AuditValueType == AuditValueType.Insert)
56-
{
57-
if (e.Property.Name == AuditedPropertyConst.CreationTime ||
58-
e.Property.Name == AuditedPropertyConst.ModificationTime)
59-
e.Value = DateTime.Now;
60-
}
61-
if (e.AuditValueType == AuditValueType.Update)
62-
{
63-
if (e.Property.Name == AuditedPropertyConst.ModificationTime)
64-
e.Value = DateTime.Now;
65-
}
66-
// 用户
67-
if (e.Property.Name == AuditedPropertyConst.Creator ||
68-
e.Property.Name == AuditedPropertyConst.Modifier ||
69-
e.Property.Name == AuditedPropertyConst.CreatorId ||
70-
e.Property.Name == AuditedPropertyConst.ModifierId)
44+
var freeSql = freeSqlBuilder.Build();
45+
freeSqlSetupAction?.Invoke(s, freeSql);
46+
freeSql.Aop.AuditValue += (s, e) =>
7147
{
72-
var currentUser = ServiceLocator.Instance?.GetService<ICurrentUser>();
73-
if (currentUser == null || currentUser.UserId.IsEmpty())
74-
return;
48+
// 乐观锁
49+
if (e.AuditValueType == AuditValueType.Insert || e.AuditValueType == AuditValueType.Update)
50+
{
51+
if (e.Property.Name == AuditedPropertyConst.Version)
52+
if (e.Value is byte[] bytes && bytes.Length == 0)
53+
e.Value = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
54+
}
55+
// 时间
7556
if (e.AuditValueType == AuditValueType.Insert)
7657
{
77-
if (e.Property.Name == AuditedPropertyConst.Creator ||
78-
e.Property.Name == AuditedPropertyConst.Modifier)
79-
e.Value = currentUser.GetFullName() ?? currentUser.GetUserName();
80-
if (e.Property.Name == AuditedPropertyConst.CreatorId ||
81-
e.Property.Name == AuditedPropertyConst.ModifierId)
82-
e.Value = currentUser.GetUserId();
58+
if (e.Property.Name == AuditedPropertyConst.CreationTime ||
59+
e.Property.Name == AuditedPropertyConst.ModificationTime)
60+
e.Value = DateTime.Now;
8361
}
8462
if (e.AuditValueType == AuditValueType.Update)
8563
{
86-
if (e.Property.Name == AuditedPropertyConst.Modifier)
87-
e.Value = currentUser.GetFullName() ?? currentUser.GetUserName();
88-
if (e.Property.Name == AuditedPropertyConst.ModifierId)
89-
e.Value = currentUser.GetUserId();
64+
if (e.Property.Name == AuditedPropertyConst.ModificationTime)
65+
e.Value = DateTime.Now;
9066
}
91-
}
67+
// 用户
68+
if (e.Property.Name == AuditedPropertyConst.Creator ||
69+
e.Property.Name == AuditedPropertyConst.Modifier ||
70+
e.Property.Name == AuditedPropertyConst.CreatorId ||
71+
e.Property.Name == AuditedPropertyConst.ModifierId)
72+
{
73+
var currentUser = ServiceLocator.Instance?.GetService<ICurrentUser>();
74+
if (currentUser == null || currentUser.UserId.IsEmpty())
75+
return;
76+
if (e.AuditValueType == AuditValueType.Insert)
77+
{
78+
if (e.Property.Name == AuditedPropertyConst.Creator ||
79+
e.Property.Name == AuditedPropertyConst.Modifier)
80+
e.Value = currentUser.GetFullName() ?? currentUser.GetUserName();
81+
if (e.Property.Name == AuditedPropertyConst.CreatorId ||
82+
e.Property.Name == AuditedPropertyConst.ModifierId)
83+
e.Value = currentUser.GetUserId();
84+
}
85+
if (e.AuditValueType == AuditValueType.Update)
86+
{
87+
if (e.Property.Name == AuditedPropertyConst.Modifier)
88+
e.Value = currentUser.GetFullName() ?? currentUser.GetUserName();
89+
if (e.Property.Name == AuditedPropertyConst.ModifierId)
90+
e.Value = currentUser.GetUserId();
91+
}
92+
}
93+
};
94+
95+
var wrapper = new FreeSqlWrapper { Orm = freeSql };
96+
freeSql.GlobalFilter.Apply<ISoftDelete>("SoftDelete", x => x.IsDeleted == false);
97+
return wrapper;
9298
};
93-
var freeSqlWrapper = new FreeSqlWrapper { Orm = freeSql };
94-
freeSql.GlobalFilter.Apply<ISoftDelete>("SoftDelete", x => x.IsDeleted == false);
99+
100+
95101
services.AddSingleton(freeSqlWrapper);
96-
services.AddScoped<TUnitOfWOrk, TUnitOfWorkImplementation>();
102+
services.AddScoped<TUnitOfWork, TUnitOfWorkImplementation>();
97103
return services;
98104
}
99105

framework/src/Bing/Bing/Reflection/AppDomainAllAssemblyFinder.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
@@ -7,7 +8,6 @@
78
using Bing.Extensions;
89
using Bing.Finders;
910
using Microsoft.Extensions.DependencyModel;
10-
using Microsoft.Extensions.PlatformAbstractions;
1111

1212
namespace Bing.Reflection
1313
{
@@ -72,7 +72,7 @@ select name.Substring(i, name.Length - i))
7272
}
7373
else
7474
{
75-
var path = PlatformServices.Default.Application.ApplicationBasePath;
75+
var path = AppContext.BaseDirectory;
7676
var dllNames = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly)
7777
.Concat(Directory.GetFiles(path, "*.exe", SearchOption.TopDirectoryOnly))
7878
.Select(m => m.Replace(".dll", "").Replace(".exe", ""))
@@ -120,9 +120,10 @@ protected static Assembly[] LoadAssemblies(IEnumerable<string> files)
120120
/// <param name="assemblyName">程序集名称</param>
121121
protected virtual bool Match(string assemblyName)
122122
{
123-
if (assemblyName.StartsWith($"{PlatformServices.Default.Application.ApplicationName}.Views"))
123+
var applicationName = Assembly.GetEntryAssembly().GetName().Name;
124+
if (assemblyName.StartsWith($"{applicationName}.Views"))
124125
return false;
125-
if (assemblyName.StartsWith($"{PlatformServices.Default.Application.ApplicationName}.PrecompiledViews"))
126+
if (assemblyName.StartsWith($"{applicationName}.PrecompiledViews"))
126127
return false;
127128
return Regex.IsMatch(assemblyName, SkipAssemblies, RegexOptions.IgnoreCase | RegexOptions.Compiled) == false;
128129
}

framework/src/Bing/dependency.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@
1515
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(MicrosoftPackageVersion)" />
1616
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(MicrosoftPackageVersion)" />
1717
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(MicrosoftPackageVersion)" />
18-
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
1918
</ItemGroup>
2019
</Project>

modules/admin/src/Bing.Admin.FreeSQL/Bing.Admin.FreeSQL.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
<ItemGroup>
1010
<PackageReference Include="AspectCore.Extensions.Hosting" Version="2.2.0" />
11-
<PackageReference Include="Bing.Extensions.Swashbuckle" Version="1.2.2" />
12-
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.6.3" />
11+
<PackageReference Include="Bing.Extensions.Swashbuckle" Version="1.5.0" />
12+
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.4.0" />
1313
<PackageReference Include="DotNetCore.CAP.Dashboard" Version="3.1.2" />
14+
<PackageReference Include="Serilog.Enrichers.Span" Version="2.0.1" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
18+
<ProjectReference Include="..\..\..\..\framework\src\Bing.Logging.Serilog\Bing.Logging.Serilog.csproj" />
19+
<ProjectReference Include="..\..\..\..\framework\src\Bing.Logging.Sinks.Exceptionless\Bing.Logging.Sinks.Exceptionless.csproj" />
1720
<ProjectReference Include="..\..\..\..\framework\src\Bing.Locks.CSRedis\Bing.Locks.CSRedis.csproj" />
1821
<ProjectReference Include="..\Bing.Admin.Data.FreeSQL\Bing.Admin.Data.FreeSQL.csproj" />
1922
<ProjectReference Include="..\Bing.Admin.EventHandlers\Bing.Admin.EventHandlers.csproj" />

modules/admin/src/Bing.Admin.FreeSQL/Modules/FreeSqlModule.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using System.ComponentModel;
1+
using System;
2+
using System.ComponentModel;
23
using Bing.Admin.Data;
4+
using Bing.Admin.Data.UnitOfWorks.MySql;
35
using Bing.Core.Modularity;
46
using Bing.Data.Enums;
57
using Bing.Datas.Dapper;
68
using Bing.FreeSQL;
79
using Microsoft.Extensions.Configuration;
810
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging;
912

1013
namespace Bing.Admin.Modules
1114
{
@@ -35,7 +38,16 @@ public override IServiceCollection AddServices(IServiceCollection services)
3538
var configuration = services.GetConfiguration();
3639
var connectionStr = configuration.GetConnectionString("DefaultConnection");
3740
// 注册工作单元
38-
services.AddMySqlUnitOfWork<IAdminUnitOfWork, Bing.Admin.Data.UnitOfWorks.MySql.AdminUnitOfWork>(connectionStr);
41+
services.AddMySqlUnitOfWork<IAdminUnitOfWork, Bing.Admin.Data.UnitOfWorks.MySql.AdminUnitOfWork>(
42+
connectionStr,
43+
(serviceProvider, builder) =>
44+
{
45+
var logger = serviceProvider.GetRequiredService<ILogger<AdminUnitOfWork>>();
46+
builder.UseMonitorCommand(executing: _ => { }, executed: (cmd, traceLog) =>
47+
{
48+
logger.LogInformation($"库名:{cmd?.Connection?.Database}{Environment.NewLine}{traceLog}");
49+
});
50+
});
3951
//services.AddMySqlUnitOfWork<IAdminReadonlyUnitOfWork, Bing.Admin.Data.UnitOfWorks.MySql.AdminReadonlyUnitOfWork>(connectionStr);
4052
// 注册SqlQuery
4153
services.AddSqlQuery<Bing.Admin.Data.UnitOfWorks.MySql.AdminUnitOfWork, Bing.Admin.Data.UnitOfWorks.MySql.AdminUnitOfWork>(options =>

modules/admin/src/Bing.Admin.FreeSQL/Modules/LogModule.cs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
using System.ComponentModel;
2+
using System.Threading;
23
using Bing.AspNetCore;
34
using Bing.Core.Modularity;
4-
using Bing.Logs.Exceptionless;
5-
using Bing.Logs.NLog;
5+
using Bing.Logging;
6+
using Bing.Logging.Serilog;
7+
using Bing.Tracing;
8+
using Exceptionless;
69
using Microsoft.Extensions.DependencyInjection;
10+
using Serilog;
11+
using Serilog.Enrichers.Span;
12+
using serilog = Serilog;
713

814
namespace Bing.Admin.Modules
915
{
@@ -32,12 +38,71 @@ public class LogModule : AspNetCoreBingModule
3238
public override IServiceCollection AddServices(IServiceCollection services)
3339
{
3440
//services.AddNLog();
35-
services.AddExceptionless(x =>
41+
// 同时输出2种方式的日志,可能存在重复 需要陆续兼容
42+
Logs.Exceptionless.Extensions.AddExceptionless(services, o =>
3643
{
37-
x.ServerUrl = "http://10.186.135.27:5100";
38-
x.ApiKey = "yIgaHwtLwbN9VoUCP0UYpSPVzwpmeNSdVSfVIVta";
44+
o.ApiKey = "N8HOaOLndl0hF7ZhOfiwJ9HmOi6kPwjKEKWLCMzE";
45+
o.ServerUrl = "http://10.186.132.40:5100";
46+
});
47+
//ExceptionlessClient.Default.Configuration.ApiKey= "ez9jumyxVxjTxqSm0oUQhCML3OGCkDfMGyW1hfmn";
48+
//ExceptionlessClient.Default.Configuration.ServerUrl = "http://10.186.132.40:5100";
49+
//ExceptionlessClient.Default.Startup();
50+
services.AddSingleton<ILogContextAccessor, LogContextAccessor>();
51+
services.AddLogging(loggingBuilder =>
52+
{
53+
var configuration = services.GetConfiguration();
54+
serilog.Log.Logger = new serilog.LoggerConfiguration()
55+
.Enrich.FromLogContext()
56+
.Enrich.WithLogContext()
57+
.Enrich.WithLogLevel()
58+
.Enrich.WithSpan()
59+
.WriteTo.Exceptionless(additionalOperation: (builder) =>
60+
{
61+
if (builder.Target.Data.TryGetValue("TraceId", out var traceId))
62+
builder.Target.AddTags(traceId.ToString() ?? string.Empty);
63+
builder.Target.AddTags((TraceIdContext.Current ??= new TraceIdContext(string.Empty)).TraceId);
64+
return builder;
65+
})
66+
.ReadFrom.Configuration(configuration)
67+
.ConfigLogLevel(configuration)
68+
.CreateLogger();
69+
loggingBuilder.AddSerilog();
3970
});
4071
return services;
4172
}
4273
}
74+
75+
/// <summary>
76+
/// 日志上下文访问器
77+
/// </summary>
78+
public class LogContextAccessor : ILogContextAccessor
79+
{
80+
/// <summary>
81+
/// 当前日志上下文
82+
/// </summary>
83+
private readonly AsyncLocal<LogContext> _currentLogContext;
84+
85+
/// <summary>
86+
/// 初始化一个<see cref="LogContextAccessor"/>类型的实例
87+
/// </summary>
88+
public LogContextAccessor()
89+
{
90+
_currentLogContext = new AsyncLocal<LogContext>();
91+
}
92+
93+
/// <summary>
94+
/// 日志上下文
95+
/// </summary>
96+
public LogContext Context
97+
{
98+
get
99+
{
100+
return _currentLogContext.Value ??= new LogContext();
101+
}
102+
set
103+
{
104+
_currentLogContext.Value = value;
105+
}
106+
}
107+
}
43108
}

modules/admin/src/Bing.Admin.Infrastructure/Bing.Admin.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Bing.Utils.Http" Version="1.2.3" />
17+
<PackageReference Include="Bing.Utils.Http" Version="1.4.0-preview-20220831-2" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

modules/admin/src/Bing.Admin/Bing.Admin.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="AspectCore.Extensions.Hosting" Version="2.2.0" />
18-
<PackageReference Include="Bing.Extensions.Swashbuckle" Version="1.2.2" />
19-
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.6.3" />
18+
<PackageReference Include="Bing.Extensions.Swashbuckle" Version="1.5.0" />
19+
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.4.0" />
2020
<PackageReference Include="DotNetCore.CAP.Dashboard" Version="3.1.2" />
21-
2221
<PackageReference Include="Serilog.Enrichers.Span" Version="2.0.1" />
2322
</ItemGroup>
2423

modules/admin/src/Bing.Admin/Modules/AppModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public override IServiceCollection AddServices(IServiceCollection services)
7979
{
8080
o.IsEnabled = true;
8181
o.Name = "Bing.Admin";
82-
o.RequestFilter.Add("/swagger/**");
83-
o.RequestFilter.Add("/swagger");
82+
//o.RequestFilter.Add("/swagger/**");
83+
//o.RequestFilter.Add("/swagger");
8484
});
8585
return services;
8686
}

0 commit comments

Comments
 (0)