mirror of
https://github.com/dotnetcore/Util.git
synced 2026-10-26 17:53:30 +08:00
修改Exceptionless 日志配置方法为 AddExceptionless
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<VersionMajor>7</VersionMajor>
|
||||
<VersionMinor>1</VersionMinor>
|
||||
<VersionPatch>70</VersionPatch>
|
||||
<VersionPatch>73</VersionPatch>
|
||||
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
|
||||
<VersionSuffix></VersionSuffix>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Util.Aop;
|
||||
using Util.Domain;
|
||||
using Util.Validation;
|
||||
using Util.Domain;
|
||||
|
||||
namespace Util.Data.Stores;
|
||||
|
||||
|
||||
@@ -1347,11 +1347,11 @@ public abstract class SqlQueryBase : ISqlQuery, ISqlPartAccessor, ISqlOptionsAcc
|
||||
if ( Logger.IsEnabled( LogLevel.Trace ) == false )
|
||||
return;
|
||||
var message = new StringBuilder();
|
||||
message.AppendLine( "标题: {Caption}" );
|
||||
message.AppendLine( "标题: Sql日志" );
|
||||
message.AppendLine( "原始Sql:" );
|
||||
message.AppendLine( "{Sql}" );
|
||||
message.AppendLine( PreviousSql.GetSql() );
|
||||
message.AppendLine( "调试Sql:" );
|
||||
message.AppendLine( "{DebugSql}" );
|
||||
message.AppendLine( PreviousSql.GetDebugSql() );
|
||||
message.AppendLine( "Sql参数:" );
|
||||
foreach ( var param in PreviousSql.GetParams() ) {
|
||||
message.Append( "参数名: " );
|
||||
@@ -1382,7 +1382,7 @@ public abstract class SqlQueryBase : ISqlQuery, ISqlPartAccessor, ISqlOptionsAcc
|
||||
}
|
||||
message.AppendLine();
|
||||
}
|
||||
Logger.LogTrace( message.ToString(), "Sql日志", PreviousSql.GetSql(), PreviousSql.GetDebugSql() );
|
||||
Logger.LogTrace( message.ToString() );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
using Util.Data.Dapper.TypeHandlers;
|
||||
using Util.Infrastructure;
|
||||
|
||||
namespace Util.Data.Dapper.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Oracle Dapper服务注册器
|
||||
/// </summary>
|
||||
public class OracleDapperServiceRegistrar : IServiceRegistrar {
|
||||
/// <summary>
|
||||
/// 获取服务名
|
||||
/// </summary>
|
||||
public static string ServiceName => "Util.Data.Dapper.Infrastructure.OracleDapperServiceRegistrar";
|
||||
|
||||
/// <summary>
|
||||
/// 排序号
|
||||
/// </summary>
|
||||
public int OrderId => 812;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool Enabled => ServiceRegistrarConfig.IsEnabled( ServiceName );
|
||||
|
||||
/// <summary>
|
||||
/// 注册服务
|
||||
/// </summary>
|
||||
/// <param name="serviceContext">服务上下文</param>
|
||||
public Action Register( ServiceContext serviceContext ) {
|
||||
SqlMapper.RemoveTypeMap( typeof( Guid ) );
|
||||
SqlMapper.RemoveTypeMap( typeof( Guid? ) );
|
||||
SqlMapper.AddTypeHandler( typeof( Guid ), new GuidTypeHandler() );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
using Util.Infrastructure;
|
||||
|
||||
namespace Util.Data.Dapper.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Oracle Dapper服务注册器配置扩展
|
||||
/// </summary>
|
||||
public static class ServiceRegistrarConfigExtensions {
|
||||
/// <summary>
|
||||
/// 启用Oracle Dapper服务注册器
|
||||
/// </summary>
|
||||
/// <param name="config">服务注册器配置</param>
|
||||
public static ServiceRegistrarConfig EnableOracleDapperServiceRegistrar( this ServiceRegistrarConfig config ) {
|
||||
ServiceRegistrarConfig.Enable( OracleDapperServiceRegistrar.ServiceName );
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///禁用Oracle Dapper服务注册器
|
||||
/// </summary>
|
||||
/// <param name="config">服务注册器配置</param>
|
||||
public static ServiceRegistrarConfig DisableOracleDapperServiceRegistrar( this ServiceRegistrarConfig config ) {
|
||||
ServiceRegistrarConfig.Disable( OracleDapperServiceRegistrar.ServiceName );
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Util.Configs;
|
||||
using Util.Data.Dapper.TypeHandlers;
|
||||
using Util.Data.Sql;
|
||||
|
||||
namespace Util.Data.Dapper.Sql;
|
||||
@@ -61,10 +62,20 @@ public static class AppBuilderExtensions {
|
||||
builder.Host.ConfigureServices( ( context, services ) => {
|
||||
services.TryAddTransient( typeof( TService ), typeof( TImplementation ) );
|
||||
services.TryAddSingleton( typeof( SqlOptions<TImplementation> ), ( sp ) => options );
|
||||
RegisterGuidTypeHandler();
|
||||
} );
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册Guid类型处理器
|
||||
/// </summary>
|
||||
private static void RegisterGuidTypeHandler() {
|
||||
SqlMapper.RemoveTypeMap( typeof( Guid ) );
|
||||
SqlMapper.RemoveTypeMap( typeof( Guid? ) );
|
||||
SqlMapper.AddTypeHandler( typeof( Guid ), new GuidTypeHandler() );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region AddOracleSqlExecutor(配置Oracle Sql执行器)
|
||||
@@ -119,6 +130,7 @@ public static class AppBuilderExtensions {
|
||||
builder.Host.ConfigureServices( ( context, services ) => {
|
||||
services.TryAddTransient( typeof( TService ), typeof( TImplementation ) );
|
||||
services.TryAddSingleton( typeof( SqlOptions<TImplementation> ), ( sp ) => options );
|
||||
RegisterGuidTypeHandler();
|
||||
} );
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -8,25 +8,64 @@ namespace Util.Logging.Serilog;
|
||||
/// </summary>
|
||||
public static class AppBuilderExtensions {
|
||||
/// <summary>
|
||||
/// 配置Serilog日志操作
|
||||
/// 配置Exceptionless日志操作
|
||||
/// </summary>
|
||||
/// <param name="builder">应用生成器</param>
|
||||
/// <param name="isClearProviders">是否清除默认设置的日志提供程序</param>
|
||||
public static IAppBuilder AddExceptionless( this IAppBuilder builder, bool isClearProviders = false ) {
|
||||
return builder.AddExceptionless( null, isClearProviders );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置Exceptionless日志操作
|
||||
/// </summary>
|
||||
/// <param name="builder">应用生成器</param>
|
||||
/// <param name="appName">应用程序名称</param>
|
||||
public static IAppBuilder AddExceptionless( this IAppBuilder builder, string appName ) {
|
||||
return builder.AddExceptionless( null, appName );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置Exceptionless日志操作
|
||||
/// </summary>
|
||||
/// <param name="builder">应用生成器</param>
|
||||
/// <param name="configAction">Exceptionless日志配置操作</param>
|
||||
/// <param name="isClearProviders">是否清除默认设置的日志提供程序</param>
|
||||
public static IAppBuilder AddSerilog( this IAppBuilder builder, Action<SerilogExceptionlessConfiguration> configAction, bool isClearProviders = false ) {
|
||||
public static IAppBuilder AddExceptionless( this IAppBuilder builder, Action<ExceptionlessConfiguration> configAction, bool isClearProviders = false ) {
|
||||
return builder.AddExceptionless( configAction, t => t.IsClearProviders = isClearProviders );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置Exceptionless日志操作
|
||||
/// </summary>
|
||||
/// <param name="builder">应用生成器</param>
|
||||
/// <param name="configAction">Exceptionless日志配置操作</param>
|
||||
/// <param name="appName">应用程序名称</param>
|
||||
public static IAppBuilder AddExceptionless( this IAppBuilder builder, Action<ExceptionlessConfiguration> configAction, string appName ) {
|
||||
return builder.AddExceptionless( configAction, t => t.Application = appName );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置Exceptionless日志操作
|
||||
/// </summary>
|
||||
/// <param name="builder">应用生成器</param>
|
||||
/// <param name="configAction">Exceptionless日志配置操作</param>
|
||||
/// <param name="setupAction">日志配置</param>
|
||||
public static IAppBuilder AddExceptionless( this IAppBuilder builder, Action<ExceptionlessConfiguration> configAction, Action<LogOptions> setupAction ) {
|
||||
builder.CheckNull( nameof( builder ) );
|
||||
var options = new LogOptions();
|
||||
setupAction?.Invoke( options );
|
||||
builder.Host.ConfigureServices( ( context, services ) => {
|
||||
var config = new SerilogExceptionlessConfiguration();
|
||||
configAction( config );
|
||||
services.AddSingleton<ILogFactory, LogFactory>();
|
||||
services.AddTransient( typeof( ILog<> ), typeof( Log<> ) );
|
||||
services.AddTransient( typeof( ILog ), t => t.GetService<ILogFactory>()?.CreateLog( "default" ) ?? NullLog.Instance );
|
||||
var configuration = context.Configuration;
|
||||
services.AddLogging( loggingBuilder => {
|
||||
if ( isClearProviders )
|
||||
if ( options.IsClearProviders )
|
||||
loggingBuilder.ClearProviders();
|
||||
ConfigExceptionless( config.ConfigAction, configuration );
|
||||
ConfigExceptionless( configAction, configuration );
|
||||
SerilogLog.Logger = new LoggerConfiguration()
|
||||
.Enrich.WithProperty( "Application", options.Application )
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithLogLevel()
|
||||
.Enrich.WithLogContext()
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace Util.Logging.Serilog;
|
||||
|
||||
/// <summary>
|
||||
/// Serilog Exceptionless日志配置
|
||||
/// </summary>
|
||||
public class SerilogExceptionlessConfiguration {
|
||||
/// <summary>
|
||||
/// 添加Exceptionless日志组件
|
||||
/// </summary>
|
||||
public void AddExceptionless() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加Exceptionless日志组件
|
||||
/// </summary>
|
||||
/// <param name="configAction">Exceptionless日志配置操作</param>
|
||||
public void AddExceptionless( Action<ExceptionlessConfiguration> configAction ) {
|
||||
ConfigAction = configAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exceptionless日志配置操作
|
||||
/// </summary>
|
||||
internal Action<ExceptionlessConfiguration> ConfigAction { get; private set; }
|
||||
}
|
||||
+22
@@ -2,6 +2,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using Util.Data.Sql;
|
||||
using Util.Tests.Configs;
|
||||
using Util.Tests.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace Util.Data.Dapper.Tests.SqlExecutor {
|
||||
@@ -24,5 +25,26 @@ namespace Util.Data.Dapper.Tests.SqlExecutor {
|
||||
var result = await _sqlExecutor.Select( "Code" ).From( "Product" ).Where( "ProductId", id ).ToStringAsync();
|
||||
Assert.Equal( TestConfig.Value, result );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试执行Sql增删改操作 - 写入扩展属性
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestExecuteAsync_2() {
|
||||
var id = Guid.NewGuid();
|
||||
var product = new Product( id ) {
|
||||
Code = TestConfig.Value,
|
||||
TestProperty1 = "a"
|
||||
};
|
||||
await _sqlExecutor
|
||||
.AppendLine( "Insert Product(ProductId,Code,ExtraProperties) " )
|
||||
.Append( "Values(@Id,@Code,@ExtraProperties)" )
|
||||
.AddDynamicParams( product )
|
||||
.AddParam( "ExtraProperties", product.GetExtraPropertiesJson() )
|
||||
.ExecuteAsync();
|
||||
var result = await _sqlExecutor.Select( "*" ).From( "Product" ).Where( "ProductId", id ).ExecuteSingleAsync<Product>();
|
||||
Assert.Equal( TestConfig.Value, result.Code );
|
||||
Assert.Equal( "a", result.TestProperty1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class Startup {
|
||||
Environment.SetDevelopment();
|
||||
} )
|
||||
.AsBuild()
|
||||
.AddSerilog( t => t.AddExceptionless() )
|
||||
.AddExceptionless()
|
||||
.AddUtil();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
}
|
||||
},
|
||||
"Exceptionless": {
|
||||
"ApiKey": "0ntTeU4QFBKCm1FIM9sjr4NKaXkXTEBeRKch5tL9",
|
||||
"ServerUrl": "http://127.0.0.1:8095",
|
||||
"ApiKey": "8JtknZBV1NRC7bdsv6SF5cbBFrMZipWMkARZxkxo",
|
||||
"ServerUrl": "http://localhost:5480",
|
||||
"QueueMaxAttempts": 1
|
||||
}
|
||||
}
|
||||
@@ -192,4 +192,11 @@ public class Product : AggregateRoot<Product>, IDelete, ITenant, IAudited, IExtr
|
||||
public void TestDomainEvent2() {
|
||||
AddDomainEvent( new TestEvent3( Id, "a" ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取扩展属性Json
|
||||
/// </summary>
|
||||
public string GetExtraPropertiesJson() {
|
||||
return Util.Helpers.Json.ToJson( ExtraProperties );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user