Skip to content

Commit

Permalink
refactor: Send logs to the trace when the node is run by the RunCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 13, 2024
1 parent 2eb3f1a commit 9eb1cc9
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
],
"compounds": [
{
"name": "Node and Client",
"name": "Node and Client - Start",
"configurations": [
"C#: Libplanet Node - Start",
"C#: Libplanet Client - Start"
Expand Down
10 changes: 9 additions & 1 deletion src/client/LibplanetConsole.Client.Executable/Application.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using JSSoft.Commands;
using LibplanetConsole.Client.Executable.Commands;
using LibplanetConsole.Client.Executable.Tracers;
using LibplanetConsole.Common;
using LibplanetConsole.Logging;
using Microsoft.AspNetCore.Server.Kestrel.Core;

Expand All @@ -15,6 +14,11 @@ internal sealed class Application
new PrefixFilter("app", "LibplanetConsole."),
];

private readonly LoggingFilter[] _traceFilters =
[
new PrefixFilter("app", "LibplanetConsole."),
];

public Application(ApplicationOptions options, object[] instances)
{
var port = options.Port;
Expand All @@ -34,6 +38,10 @@ public Application(ApplicationOptions options, object[] instances)
{
services.AddLogging(options.LogPath, "client.log", _filters);
}
else
{
services.AddLogging(_traceFilters);
}

services.AddSingleton<CommandContext>();
services.AddSingleton<SystemTerminal>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.ComponentModel;
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.DataAnnotations;
using LibplanetConsole.Framework;
using LibplanetConsole.Settings;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace LibplanetConsole.Client.Executable.EntryCommands;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Serilog.Sinks.File" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Trace" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JSSoft.Terminals;
using LibplanetConsole.Blockchain;
using LibplanetConsole.Common.Extensions;

Expand Down
1 change: 0 additions & 1 deletion src/client/LibplanetConsole.Client/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Grpc.Core;
using Grpc.Net.Client;
using LibplanetConsole.Blockchain;
using LibplanetConsole.Blockchain.Grpc;
Expand Down
4 changes: 1 addition & 3 deletions src/common/LibplanetConsole.Seed/SeedNode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Net;
using Dasync.Collections;
using Dasync.Collections;
using Libplanet.Net.Messages;
using Libplanet.Net.Options;
using Libplanet.Net.Transports;
using LibplanetConsole.Common;
using Serilog;
using static LibplanetConsole.Common.EndPointUtility;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Net.Client.Configuration;
using LibplanetConsole.Common;

namespace LibplanetConsole.Evidence.Grpc;
Expand Down
10 changes: 9 additions & 1 deletion src/console/LibplanetConsole.Console.Executable/Application.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Console.Evidence;
using LibplanetConsole.Console.Executable.Commands;
using LibplanetConsole.Console.Executable.Tracers;
Expand All @@ -19,6 +18,11 @@ internal sealed class Application
new PrefixFilter("seed.log", "LibplanetConsole.Seed."),
];

private readonly LoggingFilter[] _traceFilters =
[
new PrefixFilter("app", "LibplanetConsole."),
];

public Application(ApplicationOptions options, object[] instances)
{
var port = options.Port;
Expand All @@ -39,6 +43,10 @@ public Application(ApplicationOptions options, object[] instances)
{
services.AddLogging(options.LogPath, "console.log", _filters);
}
else
{
services.AddLogging(_traceFilters);
}

services.AddSingleton<CommandContext>();
services.AddSingleton<SystemTerminal>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.DataAnnotations;
using LibplanetConsole.Framework;
using LibplanetConsole.Settings;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace LibplanetConsole.Console.Executable.EntryCommands;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Serilog.Sinks.File" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Trace" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion src/console/LibplanetConsole.Console/ClientCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Framework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace LibplanetConsole.Console;
Expand Down
2 changes: 0 additions & 2 deletions src/console/LibplanetConsole.Console/SeedService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using LibplanetConsole.Common;
using LibplanetConsole.Seed;
using Microsoft.Extensions.Hosting;
using static LibplanetConsole.Common.EndPointUtility;

namespace LibplanetConsole.Console;

Expand Down
12 changes: 11 additions & 1 deletion src/node/LibplanetConsole.Node.Executable/Application.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Logging;
using LibplanetConsole.Node.Evidence;
using LibplanetConsole.Node.Executable.Commands;
Expand All @@ -21,6 +20,13 @@ internal sealed class Application
new PrefixFilter("libplanet.log", "Libplanet."),
];

private readonly LoggingFilter[] _traceFilters =
[
new SourceContextFilter(
"app.log",
s => s.StartsWith("LibplanetConsole.") && !s.StartsWith("LibplanetConsole.Seed.")),
];

public Application(ApplicationOptions options, object[] instances)
{
var port = options.Port;
Expand All @@ -40,6 +46,10 @@ public Application(ApplicationOptions options, object[] instances)
{
services.AddLogging(options.LogPath, "node.log", _filters);
}
else
{
services.AddLogging(_traceFilters);
}

services.AddSingleton<CommandContext>();
services.AddSingleton<SystemTerminal>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Serilog.Sinks.File" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Trace" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JSSoft.Terminals;
using LibplanetConsole.Blockchain;
using LibplanetConsole.Common.Extensions;

Expand Down
1 change: 0 additions & 1 deletion src/node/LibplanetConsole.Node/SeedService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LibplanetConsole.Common;
using LibplanetConsole.Seed;
using static LibplanetConsole.Common.EndPointUtility;

namespace LibplanetConsole.Node;

Expand Down
45 changes: 45 additions & 0 deletions src/shared/LibplanetConsole.Logging/LoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ internal static class LoggingExtensions
public static IServiceCollection AddLogging(
this IServiceCollection @this, string logPath, string name, params LoggingFilter[] filters)
{
if (logPath == string.Empty)
{
throw new ArgumentException("Log path must be specified.", nameof(logPath));
}

if (File.Exists(logPath) is true)
{
throw new ArgumentException("Log path must be a directory.", nameof(logPath));
}

if (name == string.Empty)
{
throw new ArgumentException("Name must be specified.", nameof(name));
}

var loggerConfiguration = new LoggerConfiguration();
var logFilename = Path.Combine(logPath, name);
loggerConfiguration = loggerConfiguration.MinimumLevel.Debug();
Expand Down Expand Up @@ -37,4 +52,34 @@ public static IServiceCollection AddLogging(

return @this;
}

public static IServiceCollection AddLogging(
this IServiceCollection @this, params LoggingFilter[] filters)
{
var loggerConfiguration = new LoggerConfiguration();
loggerConfiguration = loggerConfiguration.MinimumLevel.Debug();

foreach (var filter in filters)
{
loggerConfiguration = loggerConfiguration
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(filter.Filter)
.WriteTo.Trace());
}

Log.Logger = loggerConfiguration.CreateLogger();
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
Log.Logger.Fatal(e.ExceptionObject as Exception, "Unhandled exception occurred.");
};

@this.AddSingleton<ILoggerFactory, LoggerFactory>();
@this.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddSerilog();
});

return @this;
}
}

0 comments on commit 9eb1cc9

Please sign in to comment.