Skip to content

Commit 67cbd15

Browse files
committed
Improve core logging
1 parent 185d009 commit 67cbd15

File tree

9 files changed

+168
-29
lines changed

9 files changed

+168
-29
lines changed

src/InEngine.Core/IO/Write.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public class Write : IWrite
1515
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.Red;
1616
public ConsoleColor LineColor { get; set; } = ConsoleColor.White;
1717
public List<string> Buffer { get; set; } = new List<string>();
18+
public bool IsBufferEnabled { get; set; }
19+
20+
public Write() : this(true)
21+
{}
22+
23+
public Write(bool isBufferEnabled)
24+
{
25+
IsBufferEnabled = isBufferEnabled;
26+
}
1827

1928
public IWrite Newline(int count = 1)
2029
{
@@ -45,14 +54,7 @@ public IWrite Line(object val)
4554

4655
public IWrite ColoredLine(object val, ConsoleColor consoleColor)
4756
{
48-
if (val == null)
49-
val = String.Empty;
50-
consoleOutputLock.WaitOne();
51-
Console.ForegroundColor = consoleColor;
52-
Console.WriteLine(val);
53-
Console.ResetColor();
54-
consoleOutputLock.ReleaseMutex();
55-
Buffer.Add(val.ToString());
57+
WriteColoredLineOrText(val, consoleColor, true);
5658
return this;
5759
}
5860

@@ -77,16 +79,28 @@ public IWrite Text(object val)
7779
}
7880

7981
public IWrite ColoredText(object val, ConsoleColor consoleColor)
82+
{
83+
WriteColoredLineOrText(val, consoleColor, false);
84+
return this;
85+
}
86+
87+
void WriteColoredLineOrText(object val, ConsoleColor consoleColor, bool writeLine)
8088
{
8189
if (val == null)
8290
val = String.Empty;
8391
consoleOutputLock.WaitOne();
8492
Console.ForegroundColor = consoleColor;
85-
Console.Write(val);
93+
if (writeLine)
94+
Console.WriteLine(val);
95+
else
96+
Console.Write(val);
8697
Console.ResetColor();
98+
if (IsBufferEnabled) {
99+
Buffer.Add(val.ToString());
100+
if (writeLine)
101+
Buffer.Add(Environment.NewLine);
102+
}
87103
consoleOutputLock.ReleaseMutex();
88-
Buffer.Add(val.ToString());
89-
return this;
90104
}
91105

92106
public string FlushBuffer()

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Folder Include="Queuing\Message\" />
3939
<Folder Include="Scheduling\LifeCycle\" />
4040
<Folder Include="LifeCycle\" />
41+
<Folder Include="Logging\" />
4142
</ItemGroup>
4243
<ItemGroup>
4344
<Reference Include="System.ComponentModel.DataAnnotations" />

src/InEngine.Core/Logging/ILog.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace InEngine.Core.Logging
4+
{
5+
public interface ILog
6+
{
7+
void Debug(object message);
8+
void Debug(Exception exception, object message);
9+
10+
void Warning(object message);
11+
void Warning(Exception exception, object message);
12+
13+
void Error(object message);
14+
void Error(Exception exception, object message);
15+
}
16+
}

src/InEngine.Core/Logging/Log.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using InEngine.Core.IO;
3+
using Newtonsoft.Json;
4+
5+
namespace InEngine.Core.Logging
6+
{
7+
public class Log : ILog
8+
{
9+
IWrite Write { get; set; } = new Write(false);
10+
11+
public void Debug(object message)
12+
{
13+
Write.Line(message.ToString());
14+
}
15+
16+
public void Debug(Exception exception, object message)
17+
{
18+
Write.Line(message.ToString());
19+
Write.Line(JsonConvert.SerializeObject(exception));
20+
}
21+
22+
public void Error(object message)
23+
{
24+
Write.Error(message.ToString());
25+
}
26+
27+
public void Error(Exception exception, object message)
28+
{
29+
Write.Error(message.ToString());
30+
Write.Error(JsonConvert.SerializeObject(exception));
31+
}
32+
33+
public void Warning(object message)
34+
{
35+
Write.Warning(message.ToString());
36+
}
37+
38+
public void Warning(Exception exception, object message)
39+
{
40+
Write.Warning(message.ToString());
41+
Write.Warning(JsonConvert.SerializeObject(exception));
42+
}
43+
}
44+
}

src/InEngine.Core/Queuing/Dequeue.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using InEngine.Core.Logging;
67

78
namespace InEngine.Core.Queuing
89
{
@@ -11,6 +12,7 @@ public class Dequeue : IDisposable
1112
IList<QueueAdapter> queueAdapters;
1213
public CancellationTokenSource CancellationTokenSource { get; set; }
1314
public QueueSettings QueueSettings { get; set; }
15+
public static ILog Log { get; set; } = new Log();
1416

1517
public Dequeue()
1618
{
@@ -23,11 +25,10 @@ public async Task StartAsync()
2325
if (QueueSettings == null)
2426
QueueSettings = InEngineSettings.Make().Queue;
2527

26-
// Create dequeue tasks for primary and secondary queues.
2728
var allTasks = new List<Task>();
28-
Console.WriteLine("Start dequeue tasks for primary queue...");
29+
Log.Debug("Start dequeue tasks for primary queue...");
2930
allTasks.AddRange(MakeTasks(true, QueueSettings.PrimaryQueueConsumers));
30-
Console.WriteLine("Start dequeue tasks for secondary queue...");
31+
Log.Debug("Start dequeue tasks for secondary queue...");
3132
allTasks.AddRange(MakeTasks(false, QueueSettings.SecondaryQueueConsumers));
3233
await Task.WhenAll(allTasks);
3334

@@ -39,7 +40,7 @@ public async Task StartAsync()
3940
IList<Task> MakeTasks(bool useSecondaryQueue = false, int numberOfTasks = 0)
4041
{
4142
return Enumerable.Range(0, numberOfTasks).Select((i) => {
42-
Console.WriteLine($"Registering Dequeuer #{i}");
43+
Log.Debug($"Registering Dequeuer #{i}");
4344
return Task.Factory.StartNew(() => {
4445
var queue = QueueAdapter.Make(useSecondaryQueue, QueueSettings);
4546
queue.Id = i;

src/InEngine.Core/ServerHost.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using InEngine.Core.Logging;
23
using InEngine.Core.Queuing;
34
using InEngine.Core.Scheduling;
45

@@ -9,29 +10,33 @@ public class ServerHost : IDisposable
910
public SuperScheduler SuperScheduler { get; set; }
1011
public Dequeue Dequeue { get; set; }
1112

12-
public ServerHost()
13-
{
14-
SuperScheduler = new SuperScheduler();
15-
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info };
16-
SuperScheduler.Initialize();
17-
Dequeue = new Dequeue();
18-
}
13+
public ServerHost() : this(null)
14+
{}
1915

20-
public void Dispose()
16+
public ServerHost(ILog log)
2117
{
22-
SuperScheduler?.Shutdown();
18+
//SuperScheduler = new SuperScheduler();
19+
//SuperScheduler.Initialize();
20+
Dequeue = new Dequeue();
21+
if (log != null)
22+
Dequeue.Log = log;
2323
}
2424

2525
public void Start()
2626
{
27-
SuperScheduler.Start();
27+
//SuperScheduler.Start();
2828
StartDequeueAsync();
2929
}
3030

3131
public async void StartDequeueAsync()
3232
{
3333
await Dequeue.StartAsync();
3434
}
35+
36+
public void Dispose()
37+
{
38+
SuperScheduler?.Shutdown();
39+
}
3540
}
3641
}
3742

src/InEngine/NLog.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
internalLogLevel="Trace" internalLogFile="nlog-internal.log">
88

99
<targets>
10-
<target name="c" xsi:type="Console" layout="${longdate} ${callsite} ${level} ${message}"/>
10+
<target name="c" xsi:type="Console" layout="${longdate} ${level} ${message}"/>
1111
</targets>
1212

1313
<rules>
14-
<!-- <logger name="*" minlevel="Debug" writeTo="c" />-->
14+
<logger name="*" minlevel="Debug" writeTo="c" />
1515
</rules>
1616
</nlog>

src/InEngine/NLogAdapter.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using InEngine.Core.Logging;
3+
using NLog;
4+
5+
6+
namespace InEngine
7+
{
8+
public class NLogAdapter : ILog
9+
{
10+
public ILogger Logger { get; set; }
11+
12+
public NLogAdapter() : this(null)
13+
{}
14+
15+
public static ILog Make(string loggerName = "InEngine")
16+
{
17+
return new NLogAdapter(LogManager.GetLogger(loggerName));
18+
}
19+
20+
public NLogAdapter(ILogger logger)
21+
{
22+
Logger = logger;
23+
}
24+
25+
public void Debug(object message)
26+
{
27+
Logger.Debug(message);
28+
}
29+
30+
public void Debug(Exception exception, object message)
31+
{
32+
Logger.Debug(exception, message.ToString());
33+
}
34+
35+
public void Error(object message)
36+
{
37+
Logger.Error(message);
38+
}
39+
40+
public void Error(Exception exception, object message)
41+
{
42+
Logger.Error(exception, message.ToString());
43+
}
44+
45+
public void Warning(object message)
46+
{
47+
Logger.Warn(message);
48+
}
49+
50+
public void Warning(Exception exception, object message)
51+
{
52+
Logger.Warn(exception, message.ToString());
53+
}
54+
}
55+
}

src/InEngine/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using InEngine.Core;
55
using Mono.Unix;
66
using Mono.Unix.Native;
7+
using NLog;
78

89
namespace InEngine
910
{
@@ -18,14 +19,16 @@ static void Main(string[] args)
1819
}
1920

2021
/// <summary>
21-
/// Start the scheduler as a service or as a CLI program in the foreground.
22+
/// Start the server as a service or as a CLI program in the foreground.
2223
/// </summary>
2324
public static void RunServer()
2425
{
2526
var isRunningUnderMono = Type.GetType("Mono.Runtime") != null;
2627

2728
if (isRunningUnderMono) {
28-
var serverHost = new ServerHost();
29+
var serverHost = new ServerHost(
30+
NLogAdapter.Make()
31+
);
2932
serverHost.Start();
3033
Console.WriteLine("CTRL+C to exit.");
3134
UnixSignal.WaitAny(new[] {

0 commit comments

Comments
 (0)