Skip to content

Commit

Permalink
Unify unhandled exceptions (neo-project#1626)
Browse files Browse the repository at this point in the history
* Core handle errors

* Unify UnhandledExceptions

* Remove empty line

* Move to NeoSystem

* Update NeoSystem.cs

* Allow log objects

* Change the source of UnhandledException

Co-authored-by: Erik Zhang <erik@neo.org>
  • Loading branch information
2 people authored and Tommo-L committed Jun 22, 2020
1 parent 04065b0 commit dfc3f63
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class NeoSystem : IDisposable
private ChannelsConfig start_message = null;
private bool suspend = false;

static NeoSystem()
{
// Unify unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

public NeoSystem(string storageEngine = null)
{
Plugin.LoadPlugins(this);
Expand All @@ -39,6 +45,11 @@ public NeoSystem(string storageEngine = null)
plugin.OnPluginsLoaded();
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Utility.Log("UnhandledException", LogLevel.Fatal, e.ExceptionObject);
}

public void Dispose()
{
foreach (var p in Plugin.Plugins)
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Plugins/ILogPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Neo.Plugins
{
public interface ILogPlugin
{
void Log(string source, LogLevel level, string message);
void Log(string source, LogLevel level, object message);
}
}
6 changes: 3 additions & 3 deletions src/neo/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven
}
catch (Exception ex)
{
Utility.Log(nameof(Plugin), LogLevel.Error, $"Failed to resolve assembly or its dependency: {ex.Message}");
Utility.Log(nameof(Plugin), LogLevel.Error, ex);
return null;
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private static void LoadPlugin(Assembly assembly)
}
catch (Exception ex)
{
Utility.Log(nameof(Plugin), LogLevel.Error, $"Failed to initialize plugin: {ex.Message}");
Utility.Log(nameof(Plugin), LogLevel.Error, ex);
}
}
}
Expand All @@ -161,7 +161,7 @@ internal static void LoadPlugins(NeoSystem system)
}
}

protected void Log(string message, LogLevel level = LogLevel.Info)
protected void Log(object message, LogLevel level = LogLevel.Info)
{
Utility.Log($"{nameof(Plugin)}:{Name}", level, message);
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class Logger : ReceiveActor
public Logger()
{
Receive<InitializeLogger>(_ => Sender.Tell(new LoggerInitialized()));
Receive<LogEvent>(e => Log(e.LogSource, (LogLevel)e.LogLevel(), e.Message.ToString()));
Receive<LogEvent>(e => Log(e.LogSource, (LogLevel)e.LogLevel(), e.Message));
}
}

Expand All @@ -31,7 +31,7 @@ public static IConfigurationRoot LoadConfig(string config)
.Build();
}

public static void Log(string source, LogLevel level, string message)
public static void Log(string source, LogLevel level, object message)
{
foreach (ILogPlugin plugin in Plugin.Loggers)
plugin.Log(source, level, message);
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Plugins/TestLogPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public TestLogPlugin() : base() { }

protected override void Configure() { }

void ILogPlugin.Log(string source, LogLevel level, string message)
void ILogPlugin.Log(string source, LogLevel level, object message)
{
Output = source + "_" + level.ToString() + "_" + message;
}
Expand Down

0 comments on commit dfc3f63

Please sign in to comment.