Skip to content

Commit

Permalink
新增SqlBuilder日志诊断扩展类库;发布2.3.0版本;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Mar 25, 2022
1 parent 8685c85 commit 00d0819
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 40 deletions.
1 change: 1 addition & 0 deletions SQLBuilder.Console/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<appSettings>
<!--数据库连接配置-->
<add key="ConnectionStrings" value="{'Base':['SqlServer','Server=.;Database=TestDb;Uid=test;Pwd=123;']}" />
<add key="SqlBuilder.EnableDiagnosticListener" value="true" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down
40 changes: 8 additions & 32 deletions SQLBuilder.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SQLBuilder.Enums;
using SQLBuilder.Extensions;
using SQLBuilder.Repositories;
using SQLBuilder.Diagnostics.Extensions;

namespace SQLBuilder
{
Expand Down Expand Up @@ -97,38 +98,13 @@ public static void Main(string[] args)
return null;
});

//日志诊断订阅
DiagnosticListener.AllListeners.Subscribe(new MyObserver<DiagnosticListener>(listener =>
{
//判断发布者的名字
if (listener.Name == DiagnosticStrings.DiagnosticListenerName)
{
//获取订阅信息
listener.Subscribe(new MyObserver<KeyValuePair<string, object>>(listenerData =>
{
Console.WriteLine($"监听名称:{listenerData.Key}");
dynamic listenerDataValue = listenerData.Value;
//执行前
if (listenerData.Key == DiagnosticStrings.BeforeExecute)
Console.WriteLine(listenerDataValue.Sql);
//执行后
if (listenerData.Key == DiagnosticStrings.AfterExecute)
Console.WriteLine($"耗时:{listenerDataValue.ElapsedMilliseconds}ms");
//资源释放
if (listenerData.Key == DiagnosticStrings.DisposeExecute)
{
var ldata = listenerDataValue as object;
var conn = ldata?.GetType().GetProperty("masterConnection")?.GetValue(ldata, null) as SqlConnection;
Console.WriteLine(conn?.State);
}
}));
}
}));
//注入SqlBuilder日志诊断
builder.RegisterSqlBuilderDiagnostic(
executeBefore: msg => Console.WriteLine(msg.Sql),
executeAfter: msg => Console.WriteLine(msg.ElapsedMilliseconds),
executeError: msg => Console.WriteLine(msg.Exception?.Message),
executeDispose: msg => Console.WriteLine(msg.MasterConnection.State),
disposeError: msg => Console.WriteLine(msg.Exception?.Message));

var container = builder.Build();

Expand Down
7 changes: 4 additions & 3 deletions SQLBuilder.Console/SQLBuilder.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=5.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.5.0.1\lib\net45\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\netstandard1.1\System.Memory.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -72,6 +69,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SQLBuilder.Diagnostics\SQLBuilder.Diagnostics.csproj">
<Project>{baffb4ad-9016-4612-a92b-3c1c31ba694c}</Project>
<Name>SQLBuilder.Diagnostics</Name>
</ProjectReference>
<ProjectReference Include="..\SQLBuilder\SQLBuilder.csproj">
<Project>{7f702b5c-c2ac-43ac-9600-dc1547b095aa}</Project>
<Name>SQLBuilder</Name>
Expand Down
1 change: 0 additions & 1 deletion SQLBuilder.Console/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<packages>
<package id="Autofac" version="4.9.4" targetFramework="net45" />
<package id="System.Buffers" version="4.5.1" targetFramework="net45" />
<package id="System.Diagnostics.DiagnosticSource" version="5.0.1" targetFramework="net45" />
<package id="System.Memory" version="4.5.4" targetFramework="net45" />
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net45" />
</packages>
148 changes: 148 additions & 0 deletions SQLBuilder.Diagnostics/Diagnostics/SqlBuilderDiagnosticMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#region License
/***
* Copyright © 2018-2025, 张强 (943620963@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion

using SQLBuilder.Enums;
using System;
using System.Data.Common;

namespace SQLBuilder.Diagnostics.Diagnostics
{
/// <summary>
/// SqlBuilder日志诊断执行前消息
/// </summary>
public class SqlBuilderDiagnosticBeforeMessage
{
/// <summary>
/// sql语句
/// </summary>
public string Sql { get; set; }

/// <summary>
/// json参数
/// </summary>
public string ParameterJson { get; set; }

/// <summary>
/// 数据库类型
/// </summary>
public DatabaseType DatabaseType { get; set; }

/// <summary>
/// 数据源
/// </summary>
public string DataSource { get; set; }

/// <summary>
/// 时间戳
/// </summary>
public long? Timespan { get; set; }

/// <summary>
/// 操作id
/// </summary>
public string OperationId { get; set; }

/// <summary>
/// 执行时间
/// </summary>
public DateTime ExecuteBefore { get; set; }
}

/// <summary>
/// SqlBuilder日志诊断执行后消息
/// </summary>
public class SqlBuilderDiagnosticAfterMessage
{
/// <summary>
/// sql语句
/// </summary>
public string Sql { get; set; }

/// <summary>
/// json参数
/// </summary>
public string ParameterJson { get; set; }

/// <summary>
/// 数据源
/// </summary>
public string DataSource { get; set; }

/// <summary>
/// 操作id
/// </summary>
public string OperationId { get; set; }

/// <summary>
/// 耗时(ms)
/// </summary>
public long? ElapsedMilliseconds { get; set; }

/// <summary>
/// 执行时间
/// </summary>
public DateTime ExecuteAfter { get; set; }
}

/// <summary>
/// SqlBuilder日志诊断执行异常消息
/// </summary>
public class SqlBuilderDiagnosticErrorMessage
{
/// <summary>
/// 异常
/// </summary>
public Exception Exception { get; set; }

/// <summary>
/// 操作id
/// </summary>
public string OperationId { get; set; }

/// <summary>
/// 耗时(ms)
/// </summary>
public long? ElapsedMilliseconds { get; set; }

/// <summary>
/// 执行时间
/// </summary>
public DateTime ExecuteError { get; set; }
}

/// <summary>
/// SqlBuilder日志诊断执行数据库连接释放消息
/// </summary>
public class SqlBuilderDiagnosticDisposeMessage
{
/// <summary>
/// 主库数据库连接对象
/// </summary>
public DbConnection MasterConnection { get; set; }

/// <summary>
/// 从库数据库连接对象
/// </summary>
public DbConnection SalveConnection { get; set; }

/// <summary>
/// 执行时间
/// </summary>
public DateTime ExecuteDispose { get; set; }
}
}
61 changes: 61 additions & 0 deletions SQLBuilder.Diagnostics/Diagnostics/SqlBuilderObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#region License
/***
* Copyright © 2018-2025, 张强 (943620963@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion

using System;

namespace SQLBuilder.Diagnostics.Diagnostics
{
/// <summary>
/// IObserver泛型实现类
/// </summary>
/// <typeparam name="T"></typeparam>
public class SqlBuilderObserver<T> : IObserver<T>
{
private readonly Action<T> _next;

/// <summary>
/// 构造函数
/// </summary>
/// <param name="next"></param>
public SqlBuilderObserver(Action<T> next)
{
_next = next;
}

/// <summary>
/// 完成
/// </summary>
public void OnCompleted()
{
}

/// <summary>
/// 出错
/// </summary>
/// <param name="error"></param>
public void OnError(Exception error)
{
}

/// <summary>
/// 下一步
/// </summary>
/// <param name="value"></param>
public void OnNext(T value) => _next(value);
}
}
Loading

0 comments on commit 00d0819

Please sign in to comment.