Skip to content

Commit 6e8971d

Browse files
feat: logging
1 parent 8c87f31 commit 6e8971d

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/SiteMonitor/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
using System;
2+
using System.IO;
23

34
using Avalonia;
45
using Avalonia.ReactiveUI;
56

7+
using log4net;
8+
using log4net.Config;
9+
610
namespace SiteMonitor;
711

812
internal sealed class Program {
13+
/// <summary>
14+
/// The logger.
15+
/// </summary>
16+
private static readonly ILog LOG = LogManager.GetLogger(typeof(Program));
17+
918
// Initialization code. Don't use any Avalonia, third-party APIs or any
1019
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
1120
// yet and stuff might break.
1221
[STAThread]
1322
public static void Main(string[] args) {
23+
#if DEBUG
24+
XmlConfigurator.Configure(new FileInfo("log4net.debug.config"));
25+
#else
26+
XmlConfigurator.Configure(new FileInfo("log4net.config"));
27+
#endif
28+
29+
LOG.Info("Started application");
30+
31+
AppDomain.CurrentDomain.UnhandledException += (_, exceptArgs) => {
32+
LOG.Fatal("Unhandled exception", exceptArgs.ExceptionObject as Exception);
33+
};
34+
1435
BuildAvaloniaApp()
1536
.StartWithClassicDesktopLifetime(args);
1637
}

src/SiteMonitor/Views/MainWindow.axaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44

5+
using Avalonia;
56
using Avalonia.Controls;
67
using Avalonia.Interactivity;
78

89
using Microsoft.Extensions.DependencyInjection;
910

1011
using Nullinside.Api.Common.Desktop;
11-
#if !DEBUG
12+
#if !RELEASE
1213
using Avalonia.Threading;
1314

1415
using SiteMonitor.ViewModels;
@@ -72,12 +73,12 @@ protected override void OnInitialized() {
7273
if (serverVersion.name?.Equals(Constants.APP_VERSION, StringComparison.InvariantCultureIgnoreCase) ?? true) {
7374
// Had to add this because code clean up tools were removing the "redundant" return statement.
7475
// which was causing the check to always be ignored.
75-
#if !DEBUG
76+
#if !RELEASE
7677
return;
7778
#endif
7879
}
7980

80-
#if !DEBUG
81+
#if !RELEASE
8182
var vm = ServiceProvider?.GetRequiredService<NewVersionWindowViewModel>();
8283
if (null == vm) {
8384
return;

src/SiteMonitor/log4net.config

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<log4net>
2+
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
3+
<layout type="log4net.Layout.PatternLayout">
4+
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
5+
</layout>
6+
</appender>
7+
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
8+
<file type="log4net.Util.PatternString" value="${AppData}\\nullinside\\twitch-streaming-tools\\log.txt"/>
9+
<appendToFile value="true"/>
10+
<rollingStyle value="Size"/>
11+
<maxSizeRollBackups value="5"/>
12+
<maximumFileSize value="10MB"/>
13+
<staticLogFileName value="true"/>
14+
<layout type="log4net.Layout.PatternLayout">
15+
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
16+
</layout>
17+
</appender>
18+
19+
<root>
20+
<level value="INFO"/>
21+
<appender-ref ref="ConsoleAppender"/>
22+
<appender-ref ref="FileAppender"/>
23+
</root>
24+
</log4net>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<log4net>
2+
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
3+
<layout type="log4net.Layout.PatternLayout">
4+
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
5+
</layout>
6+
</appender>
7+
8+
<root>
9+
<level value="DEBUG"/>
10+
<appender-ref ref="ConsoleAppender"/>
11+
</root>
12+
</log4net>

0 commit comments

Comments
 (0)