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

Unify unhandled exceptions #1626

Merged
merged 8 commits into from
May 4, 2020
Merged
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
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