Skip to content

Commit 35ed764

Browse files
committed
Add minor logger for error logging
1 parent dbbb730 commit 35ed764

File tree

7 files changed

+84
-18
lines changed

7 files changed

+84
-18
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using VattenMedia.Core.Entities;
2+
3+
namespace VattenMedia.Core.Interfaces
4+
{
5+
public interface ILogger
6+
{
7+
void LogError(string message);
8+
}
9+
}

VattenMedia.Core/Interfaces/IStatusTextService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace VattenMedia.Core.Interfaces
44
{
55
public interface IStatusTextService
66
{
7-
void SetCallback(Action<string, TimeSpan?> changeStatus);
8-
void ChangeStatusText(string status, TimeSpan? time = null);
7+
void SetCallback(Action<string, TimeSpan?, bool> changeStatus);
8+
void ChangeStatusText(string status, TimeSpan? time = null, bool isError = false);
99
}
1010
}

VattenMedia.Infrastructure/Extensions/ContainerExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void AddInfrastructureRegistrations(this IUnityContainer container
1616
container.RegisterType<ITwitchService, TwitchService>();
1717
container.RegisterType<IYoutubeService, YoutubeService>();
1818
container.RegisterType<ITwitchChatClientFactory, TwitchChatClientFactory>();
19+
container.RegisterType<ILogger, Logger>();
1920
}
2021
}
2122
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.IO;
3+
using VattenMedia.Core.Interfaces;
4+
5+
namespace VattenMedia.Infrastructure.Services
6+
{
7+
public class Logger : ILogger
8+
{
9+
private string LogFileTodayDate => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToShortDateString() + ".log");
10+
11+
public void LogError(string message)
12+
{
13+
var logFile = GetLogFile();
14+
15+
using StreamWriter sw = File.AppendText(logFile);
16+
sw.WriteLine($"{DateTime.Now} - Error: {message}");
17+
}
18+
19+
private string GetLogFile()
20+
{
21+
var logFile = LogFileTodayDate;
22+
23+
if (!File.Exists(logFile))
24+
{
25+
using (File.Create(logFile)) { };
26+
}
27+
28+
return logFile;
29+
}
30+
}
31+
}

VattenMedia.Infrastructure/Services/StatusTextService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ namespace VattenMedia.Infrastructure.Services
55
{
66
internal class StatusTextService : IStatusTextService
77
{
8-
private Action<string, TimeSpan?> changeStatus;
8+
private Action<string, TimeSpan?, bool> changeStatus;
99

1010
public StatusTextService()
1111
{
1212
}
1313

14-
public void SetCallback(Action<string, TimeSpan?> changeStatus)
14+
public void SetCallback(Action<string, TimeSpan?, bool> changeStatus)
1515
{
16-
this.changeStatus = changeStatus;
16+
this.changeStatus = changeStatus;
1717
}
1818

19-
public void ChangeStatusText(string status, TimeSpan? time = null)
19+
public void ChangeStatusText(string status, TimeSpan? time = null, bool isError = false)
2020
{
21-
changeStatus(status, time);
21+
changeStatus(status, time, isError);
2222
}
2323
}
2424
}

VattenMedia.Infrastructure/Services/StreamStarterService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ internal class StreamStarterService : IStreamStarterService
1010
{
1111
private readonly AppConfiguration appConfiguration;
1212
private readonly IStatusTextService statusManager;
13+
private readonly ILogger logger;
1314
private int runningProcesses;
1415

1516
public event EventHandler<int> RunningProcessesChanged;
1617

17-
public StreamStarterService(IStatusTextService statusManager, AppConfiguration appConfiguration)
18+
public StreamStarterService(
19+
IStatusTextService statusManager,
20+
ILogger logger,
21+
AppConfiguration appConfiguration)
1822
{
1923
this.statusManager = statusManager;
24+
this.logger = logger;
2025
this.appConfiguration = appConfiguration;
2126
}
2227

@@ -61,7 +66,7 @@ private void StartProcess(string process, string args)
6166

6267
processTemp.Exited += ProcessTemp_Exited;
6368
processTemp.OutputDataReceived += ProcessTemp_DataReceived;
64-
processTemp.ErrorDataReceived += ProcessTemp_DataReceived;
69+
processTemp.ErrorDataReceived += ProcessTemp_DataReceivedError;
6570

6671
try
6772
{
@@ -72,7 +77,9 @@ private void StartProcess(string process, string args)
7277
}
7378
catch (Exception e)
7479
{
75-
statusManager.ChangeStatusText($"Error: Could not start process. Exception: {e}");
80+
var errorMessage = $"Error: Could not start process. Exception: {e}";
81+
statusManager.ChangeStatusText(errorMessage);
82+
logger.LogError(errorMessage);
7683
}
7784
}
7885

@@ -92,5 +99,11 @@ private void ProcessTemp_DataReceived(object sender, DataReceivedEventArgs e)
9299
{
93100
statusManager.ChangeStatusText(e.Data, TimeSpan.FromSeconds(2));
94101
}
102+
103+
private void ProcessTemp_DataReceivedError(object sender, DataReceivedEventArgs e)
104+
{
105+
statusManager.ChangeStatusText(e.Data, TimeSpan.FromSeconds(10));
106+
logger.LogError(e.Data);
107+
}
95108
}
96109
}

VattenMedia/ViewModels/MainWindowViewModel.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class MainWindowViewModel : BaseViewModel
2323
private readonly IYoutubeService youtubeService;
2424
private readonly IStatusTextService statusManager;
2525
private readonly IStreamStarterService streamStarterService;
26+
private readonly ILogger logger;
2627
private readonly AppConfiguration appConfiguration;
2728
private int runningProcesses;
2829
private Timer statusTextTimer;
@@ -58,6 +59,7 @@ public MainWindowViewModel(
5859
IYoutubeService youtubeService,
5960
IStatusTextService statusManager,
6061
IStreamStarterService streamStarterService,
62+
ILogger logger,
6163
AppConfiguration appConfiguration)
6264
{
6365
this.viewModelFactory = viewModelFactory;
@@ -67,6 +69,7 @@ public MainWindowViewModel(
6769
this.statusManager = statusManager;
6870
statusManager.SetCallback(ChangeStatusText);
6971
this.streamStarterService = streamStarterService;
72+
this.logger = logger;
7073
this.appConfiguration = appConfiguration;
7174
streamStarterService.RunningProcessesChanged +=
7275
(s, e) => { RunningProcessesChangedHandler(e); };
@@ -110,8 +113,13 @@ private void UpdateRunningProcessesText()
110113
}
111114
}
112115

113-
private void ChangeStatusText(string status, TimeSpan? time = null)
116+
private void ChangeStatusText(string status, TimeSpan? time = null, bool isError = false)
114117
{
118+
if (isError)
119+
{
120+
logger.LogError(status);
121+
}
122+
115123
if (statusTextTimer != null && statusTextTimer.Enabled && time != null)
116124
{
117125
statusTextTimer.Stop();
@@ -145,7 +153,9 @@ private async void ListVideos(string channelId)
145153
}
146154
catch (Exception e)
147155
{
148-
statusManager.ChangeStatusText($"Error: Failed to get videos from Twitch. Exception: {e}");
156+
var errorMessage = $"Error: Failed to get videos from Twitch. Exception: {e}";
157+
statusManager.ChangeStatusText(errorMessage);
158+
logger.LogError(errorMessage);
149159
}
150160
ChangeToVideoListView();
151161
}
@@ -177,7 +187,9 @@ private async void ListChannels(IStreamingService streamingService, string acces
177187
}
178188
catch (Exception e)
179189
{
180-
statusManager.ChangeStatusText($"Error: Failed to get live channels from Twitch. Exception: {e}");
190+
var errorMessage = $"Error: Failed to get live channels from Twitch. Exception: {e}";
191+
statusManager.ChangeStatusText(errorMessage);
192+
logger.LogError(errorMessage);
181193
}
182194
}
183195

@@ -189,7 +201,7 @@ private void OnLaunchCommand(object uri)
189201
}
190202
catch (Exception ex)
191203
{
192-
ChangeStatusText($"Failed to launch stream - {ex}");
204+
ChangeStatusText($"Failed to launch stream - {ex}", isError: true);
193205
}
194206
}
195207

@@ -205,15 +217,15 @@ private void OnLaunchFromUrlCommand(object _)
205217
}
206218
catch (Exception ex)
207219
{
208-
ChangeStatusText($"Failed to launch from URL - {ex}");
220+
ChangeStatusText($"Failed to launch from URL - {ex}", isError: true);
209221
}
210222
}
211223

212224
private void OnOAuthTwitchCommand(object _)
213225
{
214226
if (string.IsNullOrEmpty(configHandler.Config.TwitchClientId))
215227
{
216-
ChangeStatusText("Client id missing - cannot authenticate");
228+
ChangeStatusText("Client id missing - cannot authenticate", isError: true);
217229
}
218230
else
219231
{
@@ -281,12 +293,12 @@ private void OpenChat(string channelName)
281293
{
282294
if (string.IsNullOrEmpty(configHandler.Config.TwitchAccessToken))
283295
{
284-
ChangeStatusText("Access token missing - cannot open chat");
296+
ChangeStatusText("Access token missing - cannot open chat", isError: true);
285297
return;
286298
}
287299
if (string.IsNullOrEmpty(configHandler.Config.TwitchUsername))
288300
{
289-
ChangeStatusText("Username is missing - cannot open chat");
301+
ChangeStatusText("Username is missing - cannot open chat", isError: true);
290302
return;
291303
}
292304
var chatWindow = new ChatView { DataContext = viewModelFactory.CreateChatViewModel() };

0 commit comments

Comments
 (0)