Skip to content

Commit

Permalink
Merge pull request #219 from EasilyNET/dev
Browse files Browse the repository at this point in the history
🎨 改进mongodb命令输出效果.
  • Loading branch information
joesdu authored Aug 22, 2023
2 parents feff231 + fd8df6a commit 4dcbf0d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>8</VersionMinor>
<VersionPatch>1</VersionPatch>
<VersionPatch>2</VersionPatch>
<VersionQuality></VersionQuality>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion sample/WebApi.Test.Unit/Controllers/MongoTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public Task MongoPost()
DateTime = DateTime.Now,
TimeSpan = TimeSpan.FromMilliseconds(50000d),
DateOnly = DateOnly.FromDateTime(DateTime.Now),
TimeOnly = TimeOnly.FromDateTime(DateTime.Now)
TimeOnly = TimeOnly.FromDateTime(DateTime.Now),
NullableDateOnly = DateOnly.FromDateTime(DateTime.Now),
NullableTimeOnly = null
};
_ = db1.Test.InsertOneAsync(o);
return Task.CompletedTask;
Expand Down
10 changes: 10 additions & 0 deletions sample/WebApi.Test.Unit/Domain/MongoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ public class MongoTest
/// TimeOnly类型
/// </summary>
public TimeOnly TimeOnly { get; set; }

/// <summary>
/// DateOnly类型
/// </summary>
public DateOnly? NullableDateOnly { get; set; }

/// <summary>
/// TimeOnly类型
/// </summary>
public TimeOnly? NullableTimeOnly { get; set; }
}
57 changes: 29 additions & 28 deletions sample/WebApi.Test.Unit/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EasilyNET.Core.Misc;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Spectre;
using WebApi.Test.Unit;

Console.Title = "EasilyNET";
Expand All @@ -23,37 +24,37 @@
builder.Host.UsePropertyInjection();

// 添加Serilog配置
_ = builder.Host.UseSerilog((hbc, lc) =>
builder.Host.UseSerilog((hbc, lc) =>
{
const LogEventLevel logLevel = LogEventLevel.Information;
_ = lc.ReadFrom.Configuration(hbc.Configuration)
.MinimumLevel.Override("Microsoft", logLevel)
.MinimumLevel.Override("System", logLevel)
.Enrich.FromLogContext()
.WriteTo.Async(wt =>
{
wt.Console();
wt.Debug();
//var mongo = builder.Services.GetService<DbContext>()?.Database;
//if (mongo is not null)
//{
// wt.MongoDBBson(c =>
// {
// // 使用链接字符串
// //var connectionString = hbc.Configuration["CONNECTIONSTRINGS_MONGO"];
// //if (string.IsNullOrWhiteSpace(connectionString)) connectionString = hbc.Configuration.GetConnectionString("Mongo");
// //if (string.IsNullOrWhiteSpace(connectionString)) throw new("链接字符串不能为空");
// //var mongoUrl = new MongoUrl(connectionString);
// //var mongoDbInstance = new MongoClient(mongoUrl).GetDatabase("serilog");
lc.ReadFrom.Configuration(hbc.Configuration)
.MinimumLevel.Override("Microsoft", logLevel)
.MinimumLevel.Override("System", logLevel)
.Enrich.FromLogContext()
.WriteTo.Async(wt =>
{
wt.Debug();
wt.Spectre();
//var mongo = builder.Services.GetService<DbContext>()?.Database;
//if (mongo is not null)
//{
// wt.MongoDBBson(c =>
// {
// // 使用链接字符串
// //var connectionString = hbc.Configuration["CONNECTIONSTRINGS_MONGO"];
// //if (string.IsNullOrWhiteSpace(connectionString)) connectionString = hbc.Configuration.GetConnectionString("Mongo");
// //if (string.IsNullOrWhiteSpace(connectionString)) throw new("链接字符串不能为空");
// //var mongoUrl = new MongoUrl(connectionString);
// //var mongoDbInstance = new MongoClient(mongoUrl).GetDatabase("serilog");
// // 设置别的数据库作为日志库
// // var mongo = builder.Services.GetService<IMongoDatabase>()?.Client.GetDatabase("serilog") ?? throw new("无法从Ioc容器中获取到Mongo服务");
// // sink will use the IMongoDatabase instance provided
// c.SetMongoDatabase(mongo);
// c.SetCollectionName("serilog");
// });
//}
});
// // 设置别的数据库作为日志库
// // var mongo = builder.Services.GetService<IMongoDatabase>()?.Client.GetDatabase("serilog") ?? throw new("无法从Ioc容器中获取到Mongo服务");
// // sink will use the IMongoDatabase instance provided
// c.SetMongoDatabase(mongo);
// c.SetCollectionName("serilog");
// });
//}
});
});
var app = builder.Build();

Expand Down
18 changes: 8 additions & 10 deletions src/EasilyNET.Mongo.ConsoleDebug/ActivityEventSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace EasilyNET.Mongo.ConsoleDebug;

/// <summary>
/// 利用IEventSubscriber实现MongoDB语句输出到控制台.
/// 利用IEventSubscriber实现MongoDB语句输出到控制台,推荐测试环境使用.
/// </summary>
public sealed class ActivityEventSubscriber : IEventSubscriber
{
Expand Down Expand Up @@ -79,16 +79,16 @@ private void WritStatus(int request_id, bool success)
.SplitColumns(new Layout(new Panel(new Text(CommandJson, new(Color.Purple)))
{
Height = 45,
Header = new("Command Json", Justify.Center)
Header = new("Command", Justify.Center)
}.Collapse().Border(new RoundedBoxBorder()).NoSafeBorder().Expand())
{
MinimumSize = 64,
Size = 96
},
new Layout(new Rows(new Panel(new Calendar(DateTime.Now)
{
HeaderStyle = Style.Parse("blue bold"),
HighlightStyle = Style.Parse("yellow bold")
HeaderStyle = new(Color.Blue, decoration: Decoration.Bold),
HighlightStyle = new(Color.Pink1, decoration: Decoration.Bold)
}.AddCalendarEvent(DateTime.Today))
{
Height = 13,
Expand All @@ -109,10 +109,10 @@ private void WritStatus(int request_id, bool success)
Height = 7,
Header = new("Mongo Request Status", Justify.Center)
}.Collapse().Border(new RoundedBoxBorder()).NoSafeBorder().Expand(), new Panel(new Text("""
________________________________________
--------------------------------------
/ Only two things are infinite, \
\ the universe and human stupidity. /
----------------------------------------
--------------------------------------
^__^ O ^__^
_______/(oo) o (oo)\_______
/\/( /(__) (__)\ )\/\
Expand All @@ -122,7 +122,7 @@ private void WritStatus(int request_id, bool success)
""", new(Color.Orange1)))
{
Height = 12,
Header = new("YongGan NiuNiu", Justify.Center)
Header = new("NiuNiu", Justify.Center)
}.Collapse().Border(new RoundedBoxBorder()).NoSafeBorder().Expand()))
{
Name = "Right",
Expand All @@ -132,9 +132,7 @@ private void WritStatus(int request_id, bool success)
AnsiConsole.Write(new Panel(layout)
{
Height = 47
}.NoBorder().NoSafeBorder());
// 解决最后未换行的问题
Console.WriteLine();
}.NoBorder().NoSafeBorder().Expand());
}

#pragma warning disable IDE0051
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
<AssemblyTitle>$(PackagePrefix).Mongo.ConsoleDebug</AssemblyTitle>
<AssemblyName>$(PackagePrefix).Mongo.ConsoleDebug</AssemblyName>
<PackageId>$(PackagePrefix).Mongo.ConsoleDebug</PackageId>
<PackageTags>MongoDB</PackageTags>
<PackageTags>MongoDB;Spectre</PackageTags>
<PackageReleaseNotes>
</PackageReleaseNotes>
<RootNamespace>EasilyNET.Mongo.ConsoleDebug</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<PackageProjectUrl>https://www.nuget.org/packages/EasilyNET.Mongo.ConsoleDebug</PackageProjectUrl>
<Description>MongoDB.ConsoleDebug,用来实现将命令语句输出到控制台的功能.</Description>
<Description>用来实现将命令语句输出到控制台的功能.推荐和 Serilog.Sinks.Spectre 一起使用效果更佳</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver.Core" Version="2.21.0" />
<!-- 默认引入 Serilog.Sinks.Spectre 方便使用 -->
<PackageReference Include="Serilog.Sinks.Spectre" Version="0.4.1" />
<PackageReference Include="Spectre.Console.Json" Version="0.47.1-preview.0.11" />
</ItemGroup>

Expand Down
25 changes: 23 additions & 2 deletions src/EasilyNET.Mongo.ConsoleDebug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,28 @@ clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityE
var mongoClient = new MongoClient(clientSettings);
```

- 该库参考[SkyAPM-dotnet MongoDB](https://github.com/SkyAPM/SkyAPM-dotnet)
-
- 该库参考 [SkyAPM-dotnet MongoDB](https://github.com/SkyAPM/SkyAPM-dotnet)
- 推荐和 [Serilog.Sinks.Spectre](https://github.com/lucadecamillis/serilog-sinks-spectre) 一起使用效果最佳

###### Seilog配置例子

```csharp
// 添加Serilog配置
builder.Host.UseSerilog((hbc, lc) =>
{
const LogEventLevel logLevel = LogEventLevel.Information;
lc.ReadFrom.Configuration(hbc.Configuration)
.MinimumLevel.Override("Microsoft", logLevel)
.MinimumLevel.Override("System", logLevel)
.Enrich.FromLogContext()
.WriteTo.Async(wt =>
{
wt.Debug();
// 输出到 Spectre.Console
wt.Spectre();
});
});
```


同时参考[MongoDB.Driver.Core.Extensions.DiagnosticSources](https://github.com/jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources)

0 comments on commit 4dcbf0d

Please sign in to comment.