Skip to content

Commit 6912204

Browse files
committed
- ExceptionFormatter for Logger added
- NugetPackageTypeLoader force ReflectionTypeLoadException
1 parent ede3681 commit 6912204

12 files changed

+226
-38
lines changed

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.7.3</Version>
7+
<Version>4.8.0</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>
@@ -45,4 +45,19 @@ Contains Dependency Injection, Module Loader and some helper classes</Descriptio
4545
<Compile Remove="WebHelper.cs" />
4646
</ItemGroup>
4747

48+
<ItemGroup>
49+
<Compile Update="Properties\Resources.Designer.cs">
50+
<DesignTime>True</DesignTime>
51+
<AutoGen>True</AutoGen>
52+
<DependentUpon>Resources.resx</DependentUpon>
53+
</Compile>
54+
</ItemGroup>
55+
56+
<ItemGroup>
57+
<EmbeddedResource Update="Properties\Resources.resx">
58+
<Generator>PublicResXFileCodeGenerator</Generator>
59+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
60+
</EmbeddedResource>
61+
</ItemGroup>
62+
4863
</Project>

Core.Common.Standard/Logger/ConsoleTarget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void Write(LogEntry entry)
4040

4141
Console.WriteLine(formattedMessage.PadRight(Console.WindowWidth - 1));
4242
}
43-
catch (IOException exception)
43+
catch (IOException)
4444
{
4545
this.IsConsoleAvailable = false;
4646
Logger.Warning("Console output is not available");
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace KY.Core
5+
{
6+
public sealed class ExceptionFormatter : ExceptionFormatter<Exception>
7+
{ }
8+
9+
public class ExceptionFormatter<T> : IExceptionFormatter
10+
where T : Exception
11+
{
12+
public string Format(T exception)
13+
{
14+
StringBuilder builder = new StringBuilder();
15+
this.FormatMessage(exception, builder);
16+
this.FormatStackTrace(exception, builder);
17+
this.FormatInnerException(exception, builder);
18+
return builder.ToString();
19+
}
20+
21+
protected virtual void BeforeFormatMessage(T exception, StringBuilder builder)
22+
{ }
23+
24+
protected virtual void FormatMessage(T exception, StringBuilder builder)
25+
{
26+
this.BeforeFormatMessage(exception, builder);
27+
builder.AppendLine(exception.Message);
28+
this.AfterFormatMessage(exception, builder);
29+
}
30+
31+
protected virtual void AfterFormatMessage(T exception, StringBuilder builder)
32+
{ }
33+
34+
protected virtual void BeforeFormatStackTrace(T exception, StringBuilder builder)
35+
{ }
36+
37+
protected virtual void FormatStackTrace(T exception, StringBuilder builder)
38+
{
39+
this.BeforeFormatStackTrace(exception, builder);
40+
builder.AppendLine(exception.StackTrace);
41+
this.AfterFormatStackTrace(exception, builder);
42+
}
43+
44+
protected virtual void AfterFormatStackTrace(T exception, StringBuilder builder)
45+
{ }
46+
47+
protected virtual void BeforeFormatInnerException(T exception, StringBuilder builder)
48+
{ }
49+
50+
protected virtual void FormatInnerException(T exception, StringBuilder builder)
51+
{
52+
this.BeforeFormatInnerException(exception, builder);
53+
if (exception.InnerException != null)
54+
{
55+
builder.AppendLine(" === INNER EXCEPTION ===");
56+
builder.AppendLine($" {Logger.Extension.Format(exception.InnerException)}");
57+
}
58+
this.AfterFormatInnerException(exception, builder);
59+
}
60+
61+
protected virtual void AfterFormatInnerException(T exception, StringBuilder builder)
62+
{ }
63+
64+
string IExceptionFormatter.Format(Exception exception)
65+
{
66+
return this.Format((T)exception);
67+
}
68+
}
69+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace KY.Core
4+
{
5+
public static class LoggerExceptionFormatExtension
6+
{
7+
public static string Format(this LoggerExtension extension, Exception exception)
8+
{
9+
Type type = exception.GetType();
10+
IExceptionFormatter formatter = Logger.ExceptionFormatters.ContainsKey(type) ? Logger.ExceptionFormatters[type] : Logger.ExceptionFormatters[typeof(Exception)];
11+
return formatter.Format(exception);
12+
}
13+
}
14+
}

Core.Common.Standard/Logger/FileTarget.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public override void Write(LogEntry entry)
2020
{
2121
if (entry.Type == LogType.Error)
2222
{
23-
string formatedError = string.Format(CultureInfo.InvariantCulture, Resources.FileErrorFormat, entry.Timestamp, entry.CustomType, entry.Message);
23+
string formattedError = string.Format(CultureInfo.InvariantCulture, Resources.FileErrorFormat, entry.Timestamp, entry.CustomType, entry.Message);
2424
string errorFileName = FileSystem.Combine(this.Path, string.Format(CultureInfo.InvariantCulture, Resources.FileErrorFileName, entry.Timestamp));
25-
this.WriteFile(errorFileName, formatedError);
25+
this.WriteFile(errorFileName, formattedError);
2626
}
2727

28-
string formatedMessage = string.Format(CultureInfo.InvariantCulture, Resources.FileTraceFormat, entry.Timestamp, entry.CustomType, entry.Message);
28+
string formattedMessage = string.Format(CultureInfo.InvariantCulture, Resources.FileTraceFormat, entry.Timestamp, entry.CustomType, entry.Message);
2929
string fileName = FileSystem.Combine(this.Path, string.Format(CultureInfo.InvariantCulture, Resources.FileTraceFileName, entry.Timestamp));
30-
this.WriteFile(fileName, formatedMessage);
30+
this.WriteFile(fileName, formattedMessage);
3131
}
3232

3333
private void WriteFile(string fileName, string text)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace KY.Core
4+
{
5+
public interface IExceptionFormatter
6+
{
7+
string Format(Exception exception);
8+
}
9+
}

Core.Common.Standard/Logger/LogEntry.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static LogEntry Error(Exception exception)
3838
{
3939
if (exception == null)
4040
return Error(Resources.EmptyException);
41-
return new LogEntry(LogType.Error, DateTime.Now, Format(exception), customType: exception.GetType().ToString());
41+
return new LogEntry(LogType.Error, DateTime.Now, Logger.Extension.Format(exception), customType: exception.GetType().ToString());
4242
}
4343

4444
public static LogEntry Error(string message, string customType = null, string source = null)
@@ -51,20 +51,5 @@ public LogEntry Unshortable()
5151
this.Shortable = false;
5252
return this;
5353
}
54-
55-
private static string Format(Exception exception)
56-
{
57-
StringBuilder builder = new StringBuilder();
58-
builder.AppendLine(exception.Message);
59-
builder.AppendLine(exception.StackTrace);
60-
61-
if (exception.InnerException != null)
62-
{
63-
builder.AppendLine(" === INNER EXCEPTION ===");
64-
builder.AppendLine(" " + Format(exception.InnerException));
65-
}
66-
67-
return builder.ToString();
68-
}
6954
}
7055
}

Core.Common.Standard/Logger/Logger.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
namespace KY.Core
@@ -15,12 +16,14 @@ public static event EventHandler<EventArgs<LogEntry>> Added
1516
public static LogTargets WarningTargets { get; }
1617
public static LogTargets ErrorTargets { get; }
1718
public static AllLogTargets AllTargets { get; }
19+
public static Dictionary<Type, IExceptionFormatter> ExceptionFormatters { get; }
1820

1921
public static ConsoleTarget Console { get; }
2022
public static FileTarget File { get; }
2123
public static EventLogTarget EventLog { get; set; }
2224
public static EventTarget Event { get; }
2325
public static VisualStudioOutputTarget VisualStudioOutput { get; }
26+
public static MsBuildOutputTarget MsBuildOutput { get; }
2427

2528
public static LoggerExtension Extension { get; }
2629
public static string Source { get; set; }
@@ -31,6 +34,7 @@ static Logger()
3134
File = new FileTarget();
3235
Event = new EventTarget();
3336
VisualStudioOutput = new VisualStudioOutputTarget();
37+
MsBuildOutput = new MsBuildOutputTarget();
3438
TraceTargets = new LogTargets();
3539
WarningTargets = new LogTargets();
3640
ErrorTargets = new LogTargets();
@@ -39,6 +43,8 @@ static Logger()
3943
AllTargets.Add(File);
4044
AllTargets.Add(Event);
4145
Extension = new LoggerExtension();
46+
ExceptionFormatters = new Dictionary<Type, IExceptionFormatter>();
47+
ExceptionFormatters[typeof(Exception)] = new ExceptionFormatter();
4248
}
4349

4450
public static void Trace(string message)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using KY.Core.Properties;
3+
4+
namespace KY.Core
5+
{
6+
public class MsBuildOutputTarget : LogTarget
7+
{
8+
public override void Write(LogEntry entry)
9+
{
10+
try
11+
{
12+
string formattedMessage;
13+
if (entry.Type == LogType.Error)
14+
{
15+
if (entry.CustomType != "Error")
16+
{
17+
formattedMessage = string.Format(Resources.MsBuildErrorFormat, entry.CustomType, entry.Message);
18+
}
19+
else
20+
{
21+
formattedMessage = string.Format(Resources.MsBuildErrorShortFormat, entry.Message);
22+
}
23+
}
24+
else if (entry.Type == LogType.Warning)
25+
{
26+
formattedMessage = string.Format(Resources.MsBuildWarningFormat, entry.Message);
27+
}
28+
else
29+
{
30+
formattedMessage = string.Format(Resources.MsBuildTraceFormat, entry.Message);
31+
}
32+
Console.WriteLine(formattedMessage);
33+
}
34+
catch
35+
{
36+
// Ignore all log related errors
37+
}
38+
}
39+
}
40+
}

Core.Common.Standard/Nuget/NugetPackageTypeLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static Type Get(string assemblyName, string nameSpace, string typeName, V
3535
Type type = assembly.GetType(name);
3636
if (type == null)
3737
{
38+
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed : Force a ReflectionTypeLoadException if something is wrong
39+
assembly.DefinedTypes.FirstOrDefault();
3840
Logger.Error($"Can not load type: {name} not found in {assembly.GetName().Name}");
3941
return null;
4042
}

0 commit comments

Comments
 (0)