Skip to content

Commit 286efeb

Browse files
committed
Added option to log to file. Added option to enable log timestamps.
1 parent 87c98a0 commit 286efeb

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

Program.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ internal static class Global
3838

3939

4040
public static bool UseIdlePriority = false;
41+
4142
public static bool ShowErrorAlerts = true;
4243
public static bool LogInitialScan = false;
44+
public static bool LogToFile = false;
45+
public static bool AddTimestampToNormalLogEntries = true;
4346

4447
public static int RetryCountOnSrcFileOpenError = 10;
4548

@@ -151,8 +154,12 @@ private static void Main()
151154

152155

153156
Global.UseIdlePriority = fileConfig.GetTextUpper("UseIdlePriority") == "TRUE"; //default is false
157+
158+
154159
Global.ShowErrorAlerts = fileConfig.GetTextUpper("ShowErrorAlerts") != "FALSE"; //default is true
155160
Global.LogInitialScan = fileConfig.GetTextUpper("LogInitialScan") == "TRUE"; //default is false
161+
Global.LogToFile = fileConfig.GetTextUpper("LogToFile") == "TRUE"; //default is false
162+
Global.AddTimestampToNormalLogEntries = fileConfig.GetTextUpper("AddTimestampToNormalLogEntries") != "FALSE"; //default is true
156163

157164

158165
Global.MaxFileSizeMB = fileConfig.GetLong("MaxFileSizeMB") ?? Global.MaxFileSizeMB;
@@ -487,7 +494,12 @@ private static async Task WriteException(Exception ex_in)
487494

488495
using (await ConsoleWatch.Lock.LockAsync(Global.CancellationToken.Token))
489496
{
490-
await FileExtensions.AppendAllTextAsync("UnhandledExceptions.log", message.ToString(), Global.CancellationToken.Token);
497+
await FileExtensions.AppendAllTextAsync
498+
(
499+
"UnhandledExceptions.log",
500+
message.ToString(),
501+
Global.CancellationToken.Token
502+
);
491503
}
492504

493505

@@ -504,16 +516,33 @@ private static async Task WriteException(Exception ex_in)
504516

505517

506518
var time = DateTime.Now;
507-
var msg = $"[{time:yyyy.MM.dd HH:mm:ss.ffff}] : {message}";
508-
await AddMessage(ConsoleColor.Red, msg, time, showAlert: true);
519+
var msg = message.ToString();
520+
await AddMessage(ConsoleColor.Red, msg, time, showAlert: true, addTimestamp: true);
509521
}
510522

511-
private static async Task AddMessage(ConsoleColor color, string message, DateTime time, bool showAlert = false)
523+
private static async Task AddMessage(ConsoleColor color, string message, DateTime time, bool showAlert = false, bool addTimestamp = false, CancellationToken? token = null, bool suppressLogFile = false)
512524
{
525+
if (addTimestamp || Global.AddTimestampToNormalLogEntries)
526+
{
527+
message = $"[{time:yyyy.MM.dd HH:mm:ss.ffff}] : {message}";
528+
}
529+
530+
513531
//await Task.Run(() =>
514532
{
515533
using (await ConsoleWatch.Lock.LockAsync(Global.CancellationToken.Token))
516534
{
535+
if (Global.LogToFile && !suppressLogFile)
536+
{
537+
await FileExtensions.AppendAllTextAsync
538+
(
539+
"Console.log",
540+
message,
541+
token ?? Global.CancellationToken.Token
542+
);
543+
}
544+
545+
517546
try
518547
{
519548
Console.ForegroundColor = color;
@@ -708,7 +737,12 @@ public static async Task WriteException(Exception ex_in, Context context)
708737

709738
using (await ConsoleWatch.Lock.LockAsync(context.Token))
710739
{
711-
await FileExtensions.AppendAllTextAsync("UnhandledExceptions.log", message.ToString(), context.Token);
740+
await FileExtensions.AppendAllTextAsync
741+
(
742+
"UnhandledExceptions.log",
743+
message.ToString(),
744+
context.Token
745+
);
712746
}
713747

714748

@@ -724,8 +758,8 @@ public static async Task WriteException(Exception ex_in, Context context)
724758
}
725759

726760

727-
var msg = $"[{context.Time.ToLocalTime():yyyy.MM.dd HH:mm:ss.ffff}] : {context.Event?.FullName} : {message}";
728-
await AddMessage(ConsoleColor.Red, msg, context, showAlert: true);
761+
var msg = $"{context.Event?.FullName} : {message}";
762+
await AddMessage(ConsoleColor.Red, msg, context, showAlert: true, addTimestamp: true);
729763
}
730764

731765
public static bool IsAsyncPath(string fullNameInvariant)
@@ -1290,12 +1324,30 @@ private static async Task OnTouchedAsync(IFileSystemEvent fse, CancellationToken
12901324
}
12911325
}
12921326

1293-
public static async Task AddMessage(ConsoleColor color, string message, Context context, bool showAlert = false)
1327+
public static async Task AddMessage(ConsoleColor color, string message, Context context, bool showAlert = false, bool addTimestamp = false)
12941328
{
1329+
if (addTimestamp || Global.AddTimestampToNormalLogEntries)
1330+
{
1331+
var time = context.Time.ToLocalTime();
1332+
message = $"[{time:yyyy.MM.dd HH:mm:ss.ffff}] : {message}";
1333+
}
1334+
1335+
12951336
//await Task.Run(() =>
12961337
{
12971338
using (await ConsoleWatch.Lock.LockAsync(context.Token))
12981339
{
1340+
if (Global.LogToFile)
1341+
{
1342+
await FileExtensions.AppendAllTextAsync
1343+
(
1344+
"Console.log",
1345+
message,
1346+
context.Token
1347+
);
1348+
}
1349+
1350+
12991351
try
13001352
{
13011353
Console.ForegroundColor = color;

appsettings.example.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
"Files": {
33

44
"UseIdlePriority": false,
5-
"ShowErrorAlerts": true,
65
"MaxFileSizeMB": 2048,
76
"RetryCountOnSrcFileOpenError": 10,
87

98

9+
"ShowErrorAlerts": true,
10+
"LogInitialScan": false,
11+
"LogToFile": false,
12+
"AddTimestampToNormalLogEntries": true,
13+
14+
1015
"Bidirectional": true,
1116
"CaseSensitiveFilenames": null,
1217

0 commit comments

Comments
 (0)