diff --git a/.vscode/launch.json b/.vscode/launch.json index 0de617bd..55d9125d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -135,7 +135,7 @@ ], "compounds": [ { - "name": "Node and Client", + "name": "Node and Client - Start", "configurations": [ "C#: Libplanet Node - Start", "C#: Libplanet Client - Start" diff --git a/src/client/LibplanetConsole.Client.Executable/Application.cs b/src/client/LibplanetConsole.Client.Executable/Application.cs index 6b4d01ee..fa0683f2 100644 --- a/src/client/LibplanetConsole.Client.Executable/Application.cs +++ b/src/client/LibplanetConsole.Client.Executable/Application.cs @@ -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; @@ -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; @@ -34,6 +38,10 @@ public Application(ApplicationOptions options, object[] instances) { services.AddLogging(options.LogPath, "client.log", _filters); } + else + { + services.AddLogging(_traceFilters); + } services.AddSingleton(); services.AddSingleton(); diff --git a/src/client/LibplanetConsole.Client.Executable/EntryCommands/StartCommand.cs b/src/client/LibplanetConsole.Client.Executable/EntryCommands/StartCommand.cs index 4671e488..f9039128 100644 --- a/src/client/LibplanetConsole.Client.Executable/EntryCommands/StartCommand.cs +++ b/src/client/LibplanetConsole.Client.Executable/EntryCommands/StartCommand.cs @@ -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; diff --git a/src/client/LibplanetConsole.Client.Executable/LibplanetConsole.Client.Executable.csproj b/src/client/LibplanetConsole.Client.Executable/LibplanetConsole.Client.Executable.csproj index c69322c2..c76fc9b5 100644 --- a/src/client/LibplanetConsole.Client.Executable/LibplanetConsole.Client.Executable.csproj +++ b/src/client/LibplanetConsole.Client.Executable/LibplanetConsole.Client.Executable.csproj @@ -11,6 +11,7 @@ + diff --git a/src/client/LibplanetConsole.Client.Executable/Tracers/BlockChainEventTracer.cs b/src/client/LibplanetConsole.Client.Executable/Tracers/BlockChainEventTracer.cs index 4bf1ef64..93f97890 100644 --- a/src/client/LibplanetConsole.Client.Executable/Tracers/BlockChainEventTracer.cs +++ b/src/client/LibplanetConsole.Client.Executable/Tracers/BlockChainEventTracer.cs @@ -1,4 +1,3 @@ -using JSSoft.Terminals; using LibplanetConsole.Blockchain; using LibplanetConsole.Common.Extensions; diff --git a/src/client/LibplanetConsole.Client/Client.cs b/src/client/LibplanetConsole.Client/Client.cs index e55bf3c6..4efe8a97 100644 --- a/src/client/LibplanetConsole.Client/Client.cs +++ b/src/client/LibplanetConsole.Client/Client.cs @@ -1,4 +1,3 @@ -using Grpc.Core; using Grpc.Net.Client; using LibplanetConsole.Blockchain; using LibplanetConsole.Blockchain.Grpc; diff --git a/src/common/LibplanetConsole.Seed/SeedNode.cs b/src/common/LibplanetConsole.Seed/SeedNode.cs index 19e7ea0a..f23dad85 100644 --- a/src/common/LibplanetConsole.Seed/SeedNode.cs +++ b/src/common/LibplanetConsole.Seed/SeedNode.cs @@ -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; diff --git a/src/console/LibplanetConsole.Console.Evidence/Grpc/EvidenceChannel.cs b/src/console/LibplanetConsole.Console.Evidence/Grpc/EvidenceChannel.cs index 3ccfdf7c..7ccefacd 100644 --- a/src/console/LibplanetConsole.Console.Evidence/Grpc/EvidenceChannel.cs +++ b/src/console/LibplanetConsole.Console.Evidence/Grpc/EvidenceChannel.cs @@ -1,6 +1,4 @@ -using Grpc.Core; using Grpc.Net.Client; -using Grpc.Net.Client.Configuration; using LibplanetConsole.Common; namespace LibplanetConsole.Evidence.Grpc; diff --git a/src/console/LibplanetConsole.Console.Executable/Application.cs b/src/console/LibplanetConsole.Console.Executable/Application.cs index 73eb9816..b3fda9d5 100644 --- a/src/console/LibplanetConsole.Console.Executable/Application.cs +++ b/src/console/LibplanetConsole.Console.Executable/Application.cs @@ -1,5 +1,4 @@ using JSSoft.Commands; -using LibplanetConsole.Common; using LibplanetConsole.Console.Evidence; using LibplanetConsole.Console.Executable.Commands; using LibplanetConsole.Console.Executable.Tracers; @@ -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; @@ -39,6 +43,10 @@ public Application(ApplicationOptions options, object[] instances) { services.AddLogging(options.LogPath, "console.log", _filters); } + else + { + services.AddLogging(_traceFilters); + } services.AddSingleton(); services.AddSingleton(); diff --git a/src/console/LibplanetConsole.Console.Executable/EntryCommands/StartCommand.cs b/src/console/LibplanetConsole.Console.Executable/EntryCommands/StartCommand.cs index f57fa8b3..3b4a1b9e 100644 --- a/src/console/LibplanetConsole.Console.Executable/EntryCommands/StartCommand.cs +++ b/src/console/LibplanetConsole.Console.Executable/EntryCommands/StartCommand.cs @@ -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; diff --git a/src/console/LibplanetConsole.Console.Executable/LibplanetConsole.Console.Executable.csproj b/src/console/LibplanetConsole.Console.Executable/LibplanetConsole.Console.Executable.csproj index 020bd0a4..e152f23e 100644 --- a/src/console/LibplanetConsole.Console.Executable/LibplanetConsole.Console.Executable.csproj +++ b/src/console/LibplanetConsole.Console.Executable/LibplanetConsole.Console.Executable.csproj @@ -9,6 +9,7 @@ + diff --git a/src/console/LibplanetConsole.Console/ClientCollection.cs b/src/console/LibplanetConsole.Console/ClientCollection.cs index 64e9b26a..4aff8d55 100644 --- a/src/console/LibplanetConsole.Console/ClientCollection.cs +++ b/src/console/LibplanetConsole.Console/ClientCollection.cs @@ -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; diff --git a/src/console/LibplanetConsole.Console/SeedService.cs b/src/console/LibplanetConsole.Console/SeedService.cs index d1943859..0816c091 100644 --- a/src/console/LibplanetConsole.Console/SeedService.cs +++ b/src/console/LibplanetConsole.Console/SeedService.cs @@ -1,7 +1,5 @@ using LibplanetConsole.Common; using LibplanetConsole.Seed; -using Microsoft.Extensions.Hosting; -using static LibplanetConsole.Common.EndPointUtility; namespace LibplanetConsole.Console; diff --git a/src/node/LibplanetConsole.Node.Executable/Application.cs b/src/node/LibplanetConsole.Node.Executable/Application.cs index 788f3cd1..c9801b00 100644 --- a/src/node/LibplanetConsole.Node.Executable/Application.cs +++ b/src/node/LibplanetConsole.Node.Executable/Application.cs @@ -1,5 +1,4 @@ using JSSoft.Commands; -using LibplanetConsole.Common; using LibplanetConsole.Logging; using LibplanetConsole.Node.Evidence; using LibplanetConsole.Node.Executable.Commands; @@ -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; @@ -40,6 +46,10 @@ public Application(ApplicationOptions options, object[] instances) { services.AddLogging(options.LogPath, "node.log", _filters); } + else + { + services.AddLogging(_traceFilters); + } services.AddSingleton(); services.AddSingleton(); diff --git a/src/node/LibplanetConsole.Node.Executable/LibplanetConsole.Node.Executable.csproj b/src/node/LibplanetConsole.Node.Executable/LibplanetConsole.Node.Executable.csproj index ed3f7fb1..07078ed2 100644 --- a/src/node/LibplanetConsole.Node.Executable/LibplanetConsole.Node.Executable.csproj +++ b/src/node/LibplanetConsole.Node.Executable/LibplanetConsole.Node.Executable.csproj @@ -9,6 +9,7 @@ + diff --git a/src/node/LibplanetConsole.Node.Executable/Tracers/BlockChainEventTracer.cs b/src/node/LibplanetConsole.Node.Executable/Tracers/BlockChainEventTracer.cs index 3d0c23d1..9d359ae3 100644 --- a/src/node/LibplanetConsole.Node.Executable/Tracers/BlockChainEventTracer.cs +++ b/src/node/LibplanetConsole.Node.Executable/Tracers/BlockChainEventTracer.cs @@ -1,4 +1,3 @@ -using JSSoft.Terminals; using LibplanetConsole.Blockchain; using LibplanetConsole.Common.Extensions; diff --git a/src/node/LibplanetConsole.Node/SeedService.cs b/src/node/LibplanetConsole.Node/SeedService.cs index b72cfd2a..e96266c5 100644 --- a/src/node/LibplanetConsole.Node/SeedService.cs +++ b/src/node/LibplanetConsole.Node/SeedService.cs @@ -1,6 +1,5 @@ using LibplanetConsole.Common; using LibplanetConsole.Seed; -using static LibplanetConsole.Common.EndPointUtility; namespace LibplanetConsole.Node; diff --git a/src/shared/LibplanetConsole.Logging/LoggingExtensions.cs b/src/shared/LibplanetConsole.Logging/LoggingExtensions.cs index 3f1f55f1..d76300f9 100644 --- a/src/shared/LibplanetConsole.Logging/LoggingExtensions.cs +++ b/src/shared/LibplanetConsole.Logging/LoggingExtensions.cs @@ -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(); @@ -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(); + @this.AddLogging(builder => + { + builder.ClearProviders(); + builder.AddSerilog(); + }); + + return @this; + } }