Skip to content

Commit 73142d7

Browse files
authored
chore: split log targets by source and level, include started and shutdown log events, add log file rotation. (#20)
- Write all logs to console target - Write Info logs to logfile target except: - Avalonia.* only log Warning and above. - Move log folder to AppData\SharpFM\Application.log. - Rotate logs every 30 days.
1 parent 1d5ea02 commit 73142d7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

SharpFM.App/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia;
2+
using NLog;
23
using System;
34

45
namespace SharpFM.App;
@@ -11,7 +12,16 @@ class Program
1112
[STAThread]
1213
public static void Main(string[] args)
1314
{
15+
var logger = LogManager
16+
.Setup()
17+
.LoadConfigurationFromFile("nlog.config")
18+
.GetCurrentClassLogger();
19+
20+
logger.Info("SharpFM has started up.");
21+
1422
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
23+
24+
logger.Info("SharpFM has shut down.");
1525
}
1626

1727
// Avalonia configuration, don't remove; also used by visual designer.

SharpFM.App/nlog.config

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,25 @@
2323

2424
<!-- write logs to file in app data folder -->
2525
<target xsi:type="File" name="logfile"
26-
fileName="${specialfolder:folder=ApplicationData}\SharpFM.log"
26+
fileName="${specialfolder:folder=ApplicationData}\SharpFM\Application.log"
27+
archiveFileName="${specialfolder:folder=ApplicationData}\SharpFM\Application-${date:format=yyyy-MM-dd}#{##}.log"
28+
archiveEvery="Day"
29+
concurrentWrites="true"
30+
archiveNumbering="Rolling"
31+
maxArchiveFiles="30"
2732
layout="${longdate}|${uppercase:${level:padding=5}}|${mdlc:item=ScopeName}|${ndlctiming:currentScope=true}|${logger}|${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=2}" />
2833
</targets>
2934

3035
<!-- rules to map from logger name to target -->
3136
<rules>
32-
<logger name="*" minlevel="Trace" writeTo="logfile,logconsole" />
37+
<!-- write all logs to console -->
38+
<logger name="*" minlevel="Trace" writeTo="logconsole" />
39+
40+
<!-- blackhole Avalonia Trace, Debug, and Info. only allow Warning, Error, Fatal to fall
41+
through to logfile. -->
42+
<logger name="Avalonia.*" minlevel="Trace" maxlevel="Info" final="true" />
43+
44+
<!-- anything remaining here is logged to the logfile that rotates every 30 days. -->
45+
<logger name="*" minlevel="Info" writeTo="logfile" />
3346
</rules>
3447
</nlog>

0 commit comments

Comments
 (0)