Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log plugin #112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ApplicationLogs/ApplicationLogs/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"PluginConfiguration": {
"Path": "ApplicationLogs_{0}"
}
"PluginConfiguration":
{
"Path": "ApplicationLogs_{0}",
"ConsoleOutput": true
}
}
51 changes: 51 additions & 0 deletions ApplicationLogs/ConsoleColorSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

namespace Neo.Plugins
{
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;

/// <summary>
/// Create a new color set with the current console colors
/// </summary>
public ConsoleColorSet() : this(Console.ForegroundColor, Console.BackgroundColor) { }

/// <summary>
/// Create a new color set
/// </summary>
/// <param name="foreground">Foreground color</param>
public ConsoleColorSet(ConsoleColor foreground) : this(foreground, Console.BackgroundColor) { }

/// <summary>
/// Create a new color set
/// </summary>
/// <param name="foreground">Foreground color</param>
/// <param name="background">Background color</param>
public ConsoleColorSet(ConsoleColor foreground, ConsoleColor background)
{
Foreground = foreground;
Background = background;
}

/// <summary>
/// Apply the current set
/// </summary>
public void Apply()
{
Console.ForegroundColor = Foreground;
Console.BackgroundColor = Background;
}
}
}
22 changes: 21 additions & 1 deletion ApplicationLogs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Neo.Plugins
{
public class LogReader : Plugin, IRpcPlugin, IPersistencePlugin
public class LogReader : Plugin, IRpcPlugin, IPersistencePlugin, ILogPlugin
{
private readonly DB db;

Expand All @@ -26,6 +26,26 @@ public LogReader()
public override void Configure()
{
Settings.Load(GetConfiguration());
}

public void Log(string source, LogLevel level, string message)
{
if (!Settings.Default.ConsoleOutput) return;

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($"[{DateTime.Now.TimeOfDay:hh\\:mm\\:ss\\.fff}] {message}");

currentColor.Apply();
}

public void PreProcess(HttpContext context, string method, JArray _params)
Expand Down
3 changes: 3 additions & 0 deletions ApplicationLogs/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ internal class Settings
{
public string Path { get; }

public bool ConsoleOutput { get; }

public static Settings Default { get; private set; }

private Settings(IConfigurationSection section)
{
this.Path = string.Format(section.GetSection("Path").Value, ProtocolSettings.Default.Magic.ToString("X8"));
this.ConsoleOutput = section.GetSection("ConsoleOutput").Get<bool>();
}

public static void Load(IConfigurationSection section)
Expand Down