From 92c76a52c8e225314b6217f662a4aa060580e183 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Tue, 12 May 2020 15:08:59 +0800 Subject: [PATCH 1/6] Added a native logger which implements ILogPlugin and added "log on" and "log off" commands to neo-cli. --- neo-cli/CLI/MainService.Node.cs | 18 ++++++ neo-cli/CLI/MainService.cs | 3 + neo-cli/Settings.cs | 14 +++++ neo-cli/SystemLog/ConsoleColorSet.cs | 53 +++++++++++++++++ neo-cli/SystemLog/Logger.cs | 89 ++++++++++++++++++++++++++++ neo-cli/config.json | 6 +- 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 neo-cli/SystemLog/ConsoleColorSet.cs create mode 100644 neo-cli/SystemLog/Logger.cs diff --git a/neo-cli/CLI/MainService.Node.cs b/neo-cli/CLI/MainService.Node.cs index dc892d0d5..b627325f5 100644 --- a/neo-cli/CLI/MainService.Node.cs +++ b/neo-cli/CLI/MainService.Node.cs @@ -86,5 +86,23 @@ private void OnShowStateCommand() Console.WriteLine(); Console.CursorVisible = true; } + + /// + /// Start the default ILogPlugin + /// + [ConsoleCommand("log on", Category = "Node Commands")] + private void OnLogOnCommand() + { + this.logger.Started = true; + } + + /// + /// Stop the default ILogPlugin + /// + [ConsoleCommand("log off", Category = "Node Commands")] + private void OnLogOffCommand() + { + this.logger.Started = false; + } } } diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 013768e31..cb7f69e75 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -12,6 +12,7 @@ using Neo.SmartContract; using Neo.SmartContract.Manifest; using Neo.SmartContract.Native; +using Neo.SystemLog; using Neo.VM; using Neo.VM.Types; using Neo.Wallets; @@ -65,6 +66,8 @@ private set protected override string Prompt => "neo"; public override string ServiceName => "NEO-CLI"; + private readonly Logger logger = new Logger(); + /// /// Constructor /// diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index 74108726c..0ec9f1f75 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -6,6 +6,7 @@ namespace Neo { public class Settings { + public LoggerSettings Logger { get; } public StorageSettings Storage { get; } public P2PSettings P2P { get; } public UnlockWalletSettings UnlockWallet { get; } @@ -39,6 +40,7 @@ public static Settings Default public Settings(IConfigurationSection section) { + this.Logger = new LoggerSettings(section.GetSection("Logger")); this.Storage = new StorageSettings(section.GetSection("Storage")); this.P2P = new P2PSettings(section.GetSection("P2P")); this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet")); @@ -46,6 +48,18 @@ public Settings(IConfigurationSection section) } } + public class LoggerSettings + { + public string Path { get; } + public bool ConsoleOutput { get; } + + public LoggerSettings(IConfigurationSection section) + { + this.Path = string.Format(section.GetSection("Path").Value, ProtocolSettings.Default.Magic.ToString("X8")); + this.ConsoleOutput = section.GetSection("ConsoleOutput").Get(); + } + } + public class StorageSettings { public string Engine { get; } diff --git a/neo-cli/SystemLog/ConsoleColorSet.cs b/neo-cli/SystemLog/ConsoleColorSet.cs new file mode 100644 index 000000000..79341c835 --- /dev/null +++ b/neo-cli/SystemLog/ConsoleColorSet.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Neo.SystemLog +{ + internal class ConsoleColorSet + { + #region Constants + + public static readonly ConsoleColorSet Debug = new ConsoleColorSet(ConsoleColor.Cyan); + public static readonly ConsoleColorSet Info = new ConsoleColorSet(ConsoleColor.White); + public static readonly ConsoleColorSet Warning = new ConsoleColorSet(ConsoleColor.Yellow); + public static readonly ConsoleColorSet Error = new ConsoleColorSet(ConsoleColor.Red); + public static readonly ConsoleColorSet Fatal = new ConsoleColorSet(ConsoleColor.Red); + + #endregion + + public ConsoleColor Foreground; + public ConsoleColor Background; + + /// + /// Create a new color set with the current console colors + /// + public ConsoleColorSet() : this(Console.ForegroundColor, Console.BackgroundColor) { } + + /// + /// Create a new color set + /// + /// Foreground color + public ConsoleColorSet(ConsoleColor foreground) : this(foreground, Console.BackgroundColor) { } + + /// + /// Create a new color set + /// + /// Foreground color + /// Background color + public ConsoleColorSet(ConsoleColor foreground, ConsoleColor background) + { + Foreground = foreground; + Background = background; + } + + /// + /// Apply the current set + /// + public void Apply() + { + Console.ForegroundColor = Foreground; + Console.BackgroundColor = Background; + } + } +} diff --git a/neo-cli/SystemLog/Logger.cs b/neo-cli/SystemLog/Logger.cs new file mode 100644 index 000000000..215be96c1 --- /dev/null +++ b/neo-cli/SystemLog/Logger.cs @@ -0,0 +1,89 @@ +using Neo.Plugins; +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using static System.IO.Path; +using Microsoft.Extensions.Configuration; + +namespace Neo.SystemLog +{ + public class Logger : Plugin, ILogPlugin + { + public override string Name => "SystemLog"; + + public bool Started { get; set; } + + public Logger() : base() + { + Started = true; // default is started to log + } + + private static void GetErrorLogs(StringBuilder sb, Exception ex) + { + sb.AppendLine(ex.GetType().ToString()); + sb.AppendLine(ex.Message); + sb.AppendLine(ex.StackTrace); + if (ex is AggregateException ex2) + { + foreach (Exception inner in ex2.InnerExceptions) + { + sb.AppendLine(); + GetErrorLogs(sb, inner); + } + } + else if (ex.InnerException != null) + { + sb.AppendLine(); + GetErrorLogs(sb, ex.InnerException); + } + } + + void ILogPlugin.Log(string source, LogLevel level, object message) + { + if (!Started) + return; + + if (message is Exception ex) + { + var sb = new StringBuilder(); + GetErrorLogs(sb, ex); + message = sb.ToString(); + } + + lock (typeof(Logger)) + { + DateTime now = DateTime.Now; + var log = $"[{now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}"; + + if (Settings.Default.Logger.ConsoleOutput) + { + var currentColor = new ConsoleColorSet(); + + switch (level) + { + case LogLevel.Debug: ConsoleColorSet.Debug.Apply(); break; + case LogLevel.Error: ConsoleColorSet.Error.Apply(); break; + case LogLevel.Fatal: ConsoleColorSet.Fatal.Apply(); break; + case LogLevel.Info: ConsoleColorSet.Info.Apply(); break; + case LogLevel.Warning: ConsoleColorSet.Warning.Apply(); break; + } + + Console.WriteLine(log); + currentColor.Apply(); + } + + if (!string.IsNullOrEmpty(Settings.Default.Logger.Path)) + { + StringBuilder sb = new StringBuilder(source); + foreach (char c in GetInvalidFileNameChars()) + sb.Replace(c, '-'); + var path = Combine(Settings.Default.Logger.Path, sb.ToString()); + Directory.CreateDirectory(path); + path = Combine(path, $"{now:yyyy-MM-dd}.log"); + File.AppendAllLines(path, new[] { $"[{level}]{log}" }); + } + } + } + } +} diff --git a/neo-cli/config.json b/neo-cli/config.json index 239c1c70e..b7c3a1b98 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -1,7 +1,11 @@ { "ApplicationConfiguration": { + "Logger": { + "Path": "SystemLogs_{0}", + "ConsoleOutput": true + }, "Storage": { - "Engine": "LevelDBStore" + "Engine": "" }, "P2P": { "Port": 10333, From 2094f9672dc3153570d127cb361a67282b664953 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Tue, 12 May 2020 15:18:39 +0800 Subject: [PATCH 2/6] Update config.json --- neo-cli/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo-cli/config.json b/neo-cli/config.json index b7c3a1b98..2b781c36d 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -2,10 +2,10 @@ "ApplicationConfiguration": { "Logger": { "Path": "SystemLogs_{0}", - "ConsoleOutput": true + "ConsoleOutput": false }, "Storage": { - "Engine": "" + "Engine": "LevelDBStore" }, "P2P": { "Port": 10333, From 5cea65173b0948d51de848060455face1eaa5ac3 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Tue, 12 May 2020 16:23:46 +0800 Subject: [PATCH 3/6] Move initialization into the MainService.Start(); Override ConfigFile in Logger. --- neo-cli/CLI/MainService.cs | 4 +++- neo-cli/SystemLog/Logger.cs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index cb7f69e75..7d079eb47 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -66,7 +66,7 @@ private set protected override string Prompt => "neo"; public override string ServiceName => "NEO-CLI"; - private readonly Logger logger = new Logger(); + private Logger logger; /// /// Constructor @@ -374,6 +374,8 @@ public async void Start(string[] args) } NeoSystem = new NeoSystem(Settings.Default.Storage.Engine); + logger = new Logger(); + foreach (var plugin in Plugin.Plugins) { // Register plugins commands diff --git a/neo-cli/SystemLog/Logger.cs b/neo-cli/SystemLog/Logger.cs index 215be96c1..cae4b1234 100644 --- a/neo-cli/SystemLog/Logger.cs +++ b/neo-cli/SystemLog/Logger.cs @@ -5,12 +5,14 @@ using System.IO; using static System.IO.Path; using Microsoft.Extensions.Configuration; +using System.Reflection; namespace Neo.SystemLog { public class Logger : Plugin, ILogPlugin { public override string Name => "SystemLog"; + public override string ConfigFile => Combine(GetDirectoryName(Assembly.GetEntryAssembly().Location), "config.json"); public bool Started { get; set; } From f07b41e3f1af178632a550c571ea79ffbea03130 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Tue, 12 May 2020 19:19:29 +0800 Subject: [PATCH 4/6] Relocate files; add new field into config.json; remove commands. --- .../ConsoleColorSet.cs | 4 ++-- neo-cli/{SystemLog => CLI}/Logger.cs | 5 +++-- neo-cli/CLI/MainService.Node.cs | 18 ------------------ neo-cli/CLI/MainService.cs | 1 - neo-cli/Settings.cs | 2 ++ neo-cli/config.json | 3 ++- neo-cli/config.mainnet.json | 5 +++++ neo-cli/config.testnet.json | 5 +++++ 8 files changed, 19 insertions(+), 24 deletions(-) rename {neo-cli/SystemLog => Neo.ConsoleService}/ConsoleColorSet.cs (96%) rename neo-cli/{SystemLog => CLI}/Logger.cs (95%) diff --git a/neo-cli/SystemLog/ConsoleColorSet.cs b/Neo.ConsoleService/ConsoleColorSet.cs similarity index 96% rename from neo-cli/SystemLog/ConsoleColorSet.cs rename to Neo.ConsoleService/ConsoleColorSet.cs index 79341c835..9e8225efd 100644 --- a/neo-cli/SystemLog/ConsoleColorSet.cs +++ b/Neo.ConsoleService/ConsoleColorSet.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Text; -namespace Neo.SystemLog +namespace Neo.ConsoleService { - internal class ConsoleColorSet + public class ConsoleColorSet { #region Constants diff --git a/neo-cli/SystemLog/Logger.cs b/neo-cli/CLI/Logger.cs similarity index 95% rename from neo-cli/SystemLog/Logger.cs rename to neo-cli/CLI/Logger.cs index cae4b1234..df2de0677 100644 --- a/neo-cli/SystemLog/Logger.cs +++ b/neo-cli/CLI/Logger.cs @@ -6,8 +6,9 @@ using static System.IO.Path; using Microsoft.Extensions.Configuration; using System.Reflection; +using Neo.ConsoleService; -namespace Neo.SystemLog +namespace Neo.CLI { public class Logger : Plugin, ILogPlugin { @@ -18,7 +19,7 @@ public class Logger : Plugin, ILogPlugin public Logger() : base() { - Started = true; // default is started to log + Started = Settings.Default.Logger.Started; // default is started to log } private static void GetErrorLogs(StringBuilder sb, Exception ex) diff --git a/neo-cli/CLI/MainService.Node.cs b/neo-cli/CLI/MainService.Node.cs index b627325f5..dc892d0d5 100644 --- a/neo-cli/CLI/MainService.Node.cs +++ b/neo-cli/CLI/MainService.Node.cs @@ -86,23 +86,5 @@ private void OnShowStateCommand() Console.WriteLine(); Console.CursorVisible = true; } - - /// - /// Start the default ILogPlugin - /// - [ConsoleCommand("log on", Category = "Node Commands")] - private void OnLogOnCommand() - { - this.logger.Started = true; - } - - /// - /// Stop the default ILogPlugin - /// - [ConsoleCommand("log off", Category = "Node Commands")] - private void OnLogOffCommand() - { - this.logger.Started = false; - } } } diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 7d079eb47..8a2d9499f 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -12,7 +12,6 @@ using Neo.SmartContract; using Neo.SmartContract.Manifest; using Neo.SmartContract.Native; -using Neo.SystemLog; using Neo.VM; using Neo.VM.Types; using Neo.Wallets; diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index 0ec9f1f75..3fc7a45f8 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -52,11 +52,13 @@ public class LoggerSettings { public string Path { get; } public bool ConsoleOutput { get; } + public bool Started { get; } public LoggerSettings(IConfigurationSection section) { this.Path = string.Format(section.GetSection("Path").Value, ProtocolSettings.Default.Magic.ToString("X8")); this.ConsoleOutput = section.GetSection("ConsoleOutput").Get(); + this.Started = section.GetSection("Started").Get(); } } diff --git a/neo-cli/config.json b/neo-cli/config.json index 2b781c36d..44921af0d 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -2,7 +2,8 @@ "ApplicationConfiguration": { "Logger": { "Path": "SystemLogs_{0}", - "ConsoleOutput": false + "ConsoleOutput": false, + "Started": false }, "Storage": { "Engine": "LevelDBStore" diff --git a/neo-cli/config.mainnet.json b/neo-cli/config.mainnet.json index 239c1c70e..44921af0d 100644 --- a/neo-cli/config.mainnet.json +++ b/neo-cli/config.mainnet.json @@ -1,5 +1,10 @@ { "ApplicationConfiguration": { + "Logger": { + "Path": "SystemLogs_{0}", + "ConsoleOutput": false, + "Started": false + }, "Storage": { "Engine": "LevelDBStore" }, diff --git a/neo-cli/config.testnet.json b/neo-cli/config.testnet.json index d310712ee..7a769cede 100644 --- a/neo-cli/config.testnet.json +++ b/neo-cli/config.testnet.json @@ -1,5 +1,10 @@ { "ApplicationConfiguration": { + "Logger": { + "Path": "SystemLogs_{0}", + "ConsoleOutput": false, + "Started": false + }, "Storage": { "Engine": "LevelDBStore" }, From 183907539aca78564606bcdd3dc6d05d6ecbc694 Mon Sep 17 00:00:00 2001 From: joeqian Date: Wed, 13 May 2020 14:05:32 +0800 Subject: [PATCH 5/6] Add try catch block. Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- neo-cli/CLI/Logger.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/neo-cli/CLI/Logger.cs b/neo-cli/CLI/Logger.cs index df2de0677..76bc1d7be 100644 --- a/neo-cli/CLI/Logger.cs +++ b/neo-cli/CLI/Logger.cs @@ -84,7 +84,15 @@ void ILogPlugin.Log(string source, LogLevel level, object message) var path = Combine(Settings.Default.Logger.Path, sb.ToString()); Directory.CreateDirectory(path); path = Combine(path, $"{now:yyyy-MM-dd}.log"); - File.AppendAllLines(path, new[] { $"[{level}]{log}" }); + try + { + File.AppendAllLines(path, new[] { $"[{level}]{log}" }); + } + catch (IOException) + { + Console.WriteLine("Error writing the log file: " + path); + + } } } } From 06d290217053e63cdd4ca161a0dbd2a15ecb5b87 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Wed, 13 May 2020 15:10:37 +0800 Subject: [PATCH 6/6] Remove unused usings. --- Neo.ConsoleService/ConsoleColorSet.cs | 2 -- neo-cli/CLI/Logger.cs | 3 --- neo-cli/CLI/MainService.cs | 3 ++- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Neo.ConsoleService/ConsoleColorSet.cs b/Neo.ConsoleService/ConsoleColorSet.cs index 9e8225efd..9eb2e8a29 100644 --- a/Neo.ConsoleService/ConsoleColorSet.cs +++ b/Neo.ConsoleService/ConsoleColorSet.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Neo.ConsoleService { diff --git a/neo-cli/CLI/Logger.cs b/neo-cli/CLI/Logger.cs index 76bc1d7be..2b5b32dc9 100644 --- a/neo-cli/CLI/Logger.cs +++ b/neo-cli/CLI/Logger.cs @@ -1,10 +1,8 @@ using Neo.Plugins; using System; -using System.Collections.Generic; using System.Text; using System.IO; using static System.IO.Path; -using Microsoft.Extensions.Configuration; using System.Reflection; using Neo.ConsoleService; @@ -91,7 +89,6 @@ void ILogPlugin.Log(string source, LogLevel level, object message) catch (IOException) { Console.WriteLine("Error writing the log file: " + path); - } } } diff --git a/neo-cli/CLI/MainService.cs b/neo-cli/CLI/MainService.cs index 8a2d9499f..5d45dfb3b 100644 --- a/neo-cli/CLI/MainService.cs +++ b/neo-cli/CLI/MainService.cs @@ -371,10 +371,11 @@ public async void Start(string[] args) Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config.mainnet.json").Build()); break; } - NeoSystem = new NeoSystem(Settings.Default.Storage.Engine); logger = new Logger(); + NeoSystem = new NeoSystem(Settings.Default.Storage.Engine); + foreach (var plugin in Plugin.Plugins) { // Register plugins commands