Skip to content

Commit 79960ba

Browse files
Merge pull request #75 from nullinside-development-group/feat/logging
feat: adding logging
2 parents df30df5 + 070edae commit 79960ba

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

src/TwitchStreamingTools/Program.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
using System;
2+
using System.IO;
23
using System.Runtime.Versioning;
34

45
using Avalonia;
56
using Avalonia.ReactiveUI;
67

8+
using log4net;
9+
using log4net.Config;
10+
711
[assembly: SupportedOSPlatform("windows")]
812

913
namespace TwitchStreamingTools;
1014

1115
internal sealed class Program {
16+
/// <summary>
17+
/// The logger.
18+
/// </summary>
19+
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
20+
1221
// Initialization code. Don't use any Avalonia, third-party APIs or any
1322
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
1423
// yet and stuff might break.
@@ -19,8 +28,16 @@ internal sealed class Program {
1928
/// <param name="args">The arguments passed to the application.</param>
2029
[STAThread]
2130
public static void Main(string[] args) {
22-
AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
23-
Console.Error.WriteLine(args.ExceptionObject);
31+
#if DEBUG
32+
XmlConfigurator.Configure(new FileInfo("log4net.debug.config"));
33+
#else
34+
XmlConfigurator.Configure(new FileInfo("log4net.config"));
35+
#endif
36+
37+
Log.Info("Started application");
38+
39+
AppDomain.CurrentDomain.UnhandledException += (_, exceptArgs) => {
40+
Log.Fatal("Unhandled exception", exceptArgs.ExceptionObject as Exception);
2441
};
2542

2643
BuildAvaloniaApp()

src/TwitchStreamingTools/Tts/TwitchChatTts.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Text;
77
using System.Threading;
88

9+
using log4net;
10+
911
using NAudio.Wave;
1012

1113
using Nullinside.Api.Common.Twitch;
@@ -22,6 +24,11 @@ namespace TwitchStreamingTools.Tts;
2224
/// A twitch chat text-to-speech client.
2325
/// </summary>
2426
public class TwitchChatTts : IDisposable, ITwitchChatTts {
27+
/// <summary>
28+
/// The logger.
29+
/// </summary>
30+
private static readonly ILog Log = LogManager.GetLogger(typeof(TwitchChatTts));
31+
2532
/// <summary>
2633
/// The application configuration.
2734
/// </summary>
@@ -197,12 +204,12 @@ private void PlaySoundsThread() {
197204
// If we are currently skipping messages, skip them and decrement the skipper.
198205
if (_messageToSkip > 0) {
199206
--_messageToSkip;
200-
Console.WriteLine($"Skipping: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
207+
Log.Debug($"Skipping: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
201208
continue;
202209
}
203210

204211
// Debug
205-
Console.WriteLine($"Running: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
212+
Log.Debug($"Running: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
206213

207214
// Go through the TTS filters which modify the chat message before it is passed to TTS.
208215
Tuple<string, string> convertedChatEvent = ConvertChatMessage(e);
@@ -221,7 +228,7 @@ private void PlaySoundsThread() {
221228
}
222229
}
223230
catch (Exception ex) {
224-
Console.WriteLine($"Got expection playing message: {ex}");
231+
Log.Error("Got expection playing message", ex);
225232
}
226233
}
227234
}
@@ -250,7 +257,7 @@ private void InitializeAndPlayTts(string sender, string chatMessage) {
250257
synth.Speak(chatMessage);
251258
}
252259
catch (Exception ex) {
253-
Console.WriteLine($"Exception initializing a new microsoft speech object: {ex}");
260+
Log.Error("Exception initializing a new microsoft speech object", ex);
254261
return;
255262
}
256263

@@ -344,7 +351,7 @@ private Tuple<string, string> ConvertChatMessage(OnMessageReceivedArgs chatEvent
344351
}
345352
catch (Exception ex) {
346353
// Do not let a single filter fail the loop for all filters.
347-
Console.WriteLine($"Got expection evaluating filters on message: {ex}");
354+
Log.Error("Got expection evaluating filters on message", ex);
348355
}
349356
}
350357

@@ -356,13 +363,13 @@ private Tuple<string, string> ConvertChatMessage(OnMessageReceivedArgs chatEvent
356363
/// </summary>
357364
/// <param name="e">The chat message information.</param>
358365
private void Client_OnMessageReceived(OnMessageReceivedArgs e) {
359-
Console.WriteLine($"Adding: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
366+
Log.Debug($"Adding: {e.ChatMessage.Username} says {e.ChatMessage.Message}");
360367
try {
361368
_soundsToPlay.Add(e);
362369
_twitchChatLog.AddMessage(new TwitchChatMessage(e.ChatMessage.Channel, e.ChatMessage.Username, e.ChatMessage.Message, e.GetTimestamp() ?? DateTime.UtcNow));
363370
}
364371
catch (Exception ex) {
365-
Console.WriteLine($"Failed to add: {e.ChatMessage.Username} says {e.ChatMessage.Message}\r\n{ex}");
372+
Log.Error($"Failed to add {e.ChatMessage.Username} says {e.ChatMessage.Message}", ex);
366373
}
367374
}
368375

src/TwitchStreamingTools/TwitchStreamingTools.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
<Content Include="3rdParty\soundstretch.exe">
3636
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3737
</Content>
38+
<None Remove="log4net.config"/>
39+
<Content Include="log4net.config">
40+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
41+
</Content>
42+
<None Remove="log4net.debug.config"/>
43+
<Content Include="log4net.debug.config">
44+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
45+
</Content>
3846
</ItemGroup>
3947

4048
<ItemGroup>
@@ -46,6 +54,7 @@
4654
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.1"/>
4755
<PackageReference Include="Avalonia.ReactiveUI" Version="11.3.1"/>
4856
<PackageReference Include="DynamicData" Version="9.4.1"/>
57+
<PackageReference Include="log4net" Version="3.1.0"/>
4958
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6"/>
5059
<PackageReference Include="NAudio.Wasapi" Version="2.2.1"/>
5160
<PackageReference Include="NAudio.WinMM" Version="2.2.1"/>

src/TwitchStreamingTools/ViewModels/Pages/SettingsView/TtsSkipUsernamesViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using System.Timers;
66

7+
using log4net;
8+
79
using ReactiveUI;
810

911
using TwitchLib.Api.Core.Exceptions;
@@ -21,6 +23,11 @@ namespace TwitchStreamingTools.ViewModels.Pages.SettingsView;
2123
/// Handles managing the list of users to skip in TTS.
2224
/// </summary>
2325
public class TtsSkipUsernamesViewModel : ViewModelBase {
26+
/// <summary>
27+
/// The logger.
28+
/// </summary>
29+
private static readonly ILog Log = LogManager.GetLogger(typeof(TtsSkipUsernamesViewModel));
30+
2431
/// <summary>
2532
/// The application configuration.
2633
/// </summary>
@@ -153,8 +160,7 @@ private async void UserListRefreshTimer_OnElapsed(object sender, ElapsedEventArg
153160
_doNotScanChat.Add(chat.TwitchChannel);
154161
}
155162
catch (Exception ex) {
156-
Console.Error.WriteLine($"Failed to get chatters for {chat.TwitchChannel}:");
157-
Console.Error.WriteLine(ex);
163+
Log.Error($"Failed to get chatters for {chat.TwitchChannel}:", ex);
158164
}
159165
}
160166

@@ -174,7 +180,7 @@ private async void UserListRefreshTimer_OnElapsed(object sender, ElapsedEventArg
174180
}
175181
catch (Exception ex) {
176182
// do nothing don't crash
177-
Console.Error.WriteLine(ex);
183+
Log.Error("Failed to refresh user list", ex);
178184
}
179185
}
180186
}
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)