Skip to content

Commit 55e7ce0

Browse files
authored
Merge pull request #532 from taooceros/ExceptionLogPartialOpt
Exception log partial opt
2 parents 3bbffe8 + d7ec0c1 commit 55e7ce0

File tree

9 files changed

+165
-159
lines changed

9 files changed

+165
-159
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static ICollection<PluginPair> ValidPluginsForQuery(Query query)
157157
}
158158
}
159159

160-
public static async Task<List<Result>> QueryForPlugin(PluginPair pair, Query query, CancellationToken token)
160+
public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Query query, CancellationToken token)
161161
{
162162
var results = new List<Result>();
163163
try
@@ -171,7 +171,7 @@ public static async Task<List<Result>> QueryForPlugin(PluginPair pair, Query que
171171

172172
token.ThrowIfCancellationRequested();
173173
if (results == null)
174-
return results;
174+
return null;
175175
UpdatePluginMetadata(results, metadata, query);
176176

177177
metadata.QueryCount += 1;
@@ -184,10 +184,6 @@ public static async Task<List<Result>> QueryForPlugin(PluginPair pair, Query que
184184
// null will be fine since the results will only be added into queue if the token hasn't been cancelled
185185
return null;
186186
}
187-
catch (Exception e)
188-
{
189-
Log.Exception($"|PluginManager.QueryForPlugin|Exception for plugin <{pair.Metadata.Name}> when query <{query}>", e);
190-
}
191187

192188
return results;
193189
}

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@
4949
</ItemGroup>
5050

5151
<ItemGroup>
52-
<PackageReference Include="NLog.Schema" Version="4.7.0-rc1" />
53-
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
52+
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
53+
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.10.56" />
54+
<PackageReference Include="NLog" Version="4.7.10" />
55+
<PackageReference Include="NLog.Schema" Version="4.7.10" />
56+
<PackageReference Include="NLog.Web.AspNetCore" Version="4.12.0" />
5457
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
5558
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
5659
</ItemGroup>

Flow.Launcher.Infrastructure/Logger/Log.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
using NLog.Config;
66
using NLog.Targets;
77
using Flow.Launcher.Infrastructure.UserSettings;
8+
using JetBrains.Annotations;
9+
using NLog.Fluent;
10+
using NLog.Targets.Wrappers;
11+
using System.Runtime.ExceptionServices;
12+
using System.Text;
813

914
namespace Flow.Launcher.Infrastructure.Logger
1015
{
@@ -23,23 +28,44 @@ static Log()
2328
}
2429

2530
var configuration = new LoggingConfiguration();
26-
var target = new FileTarget();
27-
configuration.AddTarget("file", target);
28-
target.FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt";
31+
32+
const string layout =
33+
@"${date:format=HH\:mm\:ss.ffffK} - " +
34+
@"${level:uppercase=true:padding=-5} - ${logger} - ${message:l}" +
35+
@"${onexception:${newline}" +
36+
@"EXCEPTION OCCURS\: ${exception:format=tostring}${newline}}";
37+
38+
var fileTarget = new FileTarget
39+
{
40+
FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt",
41+
Layout = layout
42+
};
43+
44+
var fileTargetASyncWrapper = new AsyncTargetWrapper(fileTarget);
45+
46+
var debugTarget = new OutputDebugStringTarget
47+
{
48+
Layout = layout
49+
};
50+
51+
configuration.AddTarget("file", fileTargetASyncWrapper);
52+
configuration.AddTarget("debug", debugTarget);
53+
2954
#if DEBUG
30-
var rule = new LoggingRule("*", LogLevel.Debug, target);
55+
var fileRule = new LoggingRule("*", LogLevel.Debug, fileTargetASyncWrapper);
56+
var debugRule = new LoggingRule("*", LogLevel.Debug, debugTarget);
57+
configuration.LoggingRules.Add(debugRule);
3158
#else
32-
var rule = new LoggingRule("*", LogLevel.Info, target);
59+
var fileRule = new LoggingRule("*", LogLevel.Info, fileTargetASyncWrapper);
3360
#endif
34-
configuration.LoggingRules.Add(rule);
61+
configuration.LoggingRules.Add(fileRule);
3562
LogManager.Configuration = configuration;
3663
}
3764

3865
private static void LogFaultyFormat(string message)
3966
{
4067
var logger = LogManager.GetLogger("FaultyLogger");
4168
message = $"Wrong logger message format <{message}>";
42-
System.Diagnostics.Debug.WriteLine($"FATAL|{message}");
4369
logger.Fatal(message);
4470
}
4571

@@ -51,12 +77,11 @@ private static bool FormatValid(string message)
5177
}
5278

5379

54-
55-
[MethodImpl(MethodImplOptions.Synchronized)]
5680
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
5781
{
82+
exception = exception.Demystify();
5883
#if DEBUG
59-
throw exception;
84+
ExceptionDispatchInfo.Capture(exception).Throw();
6085
#else
6186
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
6287

@@ -90,23 +115,9 @@ private static void ExceptionInternal(string classAndMethod, string message, Sys
90115
{
91116
var logger = LogManager.GetLogger(classAndMethod);
92117

93-
System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
94-
95-
logger.Error("-------------------------- Begin exception --------------------------");
96-
logger.Error(message);
118+
var messageBuilder = new StringBuilder();
97119

98-
do
99-
{
100-
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
101-
logger.Error($"Exception message:\n <{e.Message}>");
102-
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
103-
logger.Error($"Exception source:\n <{e.Source}>");
104-
logger.Error($"Exception target site:\n <{e.TargetSite}>");
105-
logger.Error($"Exception HResult:\n <{e.HResult}>");
106-
e = e.InnerException;
107-
} while (e != null);
108-
109-
logger.Error("-------------------------- End exception --------------------------");
120+
logger.Error(e, message);
110121
}
111122

112123
private static void LogInternal(string message, LogLevel level)
@@ -117,8 +128,6 @@ private static void LogInternal(string message, LogLevel level)
117128
var prefix = parts[1];
118129
var unprefixed = parts[2];
119130
var logger = LogManager.GetLogger(prefix);
120-
121-
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
122131
logger.Log(level, unprefixed);
123132
}
124133
else
@@ -128,11 +137,12 @@ private static void LogInternal(string message, LogLevel level)
128137
}
129138

130139
/// <param name="message">example: "|prefix|unprefixed" </param>
131-
[MethodImpl(MethodImplOptions.Synchronized)]
140+
/// <param name="e">Exception</param>
132141
public static void Exception(string message, System.Exception e)
133142
{
143+
e = e.Demystify();
134144
#if DEBUG
135-
throw e;
145+
ExceptionDispatchInfo.Capture(e).Throw();
136146
#else
137147
if (FormatValid(message))
138148
{
@@ -165,7 +175,6 @@ private static void LogInternal(LogLevel level, string className, string message
165175

166176
var logger = LogManager.GetLogger(classNameWithMethod);
167177

168-
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
169178
logger.Log(level, message);
170179
}
171180

Flow.Launcher/Helper/ErrorReporting.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using NLog;
44
using Flow.Launcher.Infrastructure;
55
using Flow.Launcher.Infrastructure.Exception;
6+
using NLog.Fluent;
7+
using Log = Flow.Launcher.Infrastructure.Logger.Log;
68

79
namespace Flow.Launcher.Helper
810
{
@@ -45,4 +47,4 @@ public static string DependenciesInfo()
4547
return info;
4648
}
4749
}
48-
}
50+
}

0 commit comments

Comments
 (0)