Skip to content

Commit cea810b

Browse files
committed
Added simple logging class to output errors to file
1 parent d704809 commit cea810b

File tree

7 files changed

+96
-17
lines changed

7 files changed

+96
-17
lines changed

ArtifactAPI.MatchHistory/ArtifactAPI.MatchHistory.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="Enums\MatchMode.cs" />
8080
<Compile Include="Enums\Outcome.cs" />
8181
<Compile Include="Enums\Teams.cs" />
82+
<Compile Include="Logger.cs" />
8283
<Compile Include="MatchDecoder.cs" />
8384
<Page Include="MainWindow.xaml">
8485
<Generator>MSBuild:Compile</Generator>

ArtifactAPI.MatchHistory/Converters/OutcomeToColorConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
2020
case Outcome.Draw:
2121
return Brushes.OrangeRed;
2222
default:
23-
throw new NotImplementedException("Implement case");
23+
{
24+
Logger.OutputError($"Unable to Convert outcome to color - Unknown Outcome number '{value}'");
25+
throw new NotImplementedException("Implement case");
26+
}
2427
}
2528
}
2629

ArtifactAPI.MatchHistory/Dtos/Match.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private async System.Threading.Tasks.Task LoadHeroUrl(string heroName, int heroI
125125
string url = await m_client.GetCardArtUrlAsync(heroName, ArtType.Ingame);
126126
if (string.IsNullOrEmpty(url))
127127
{
128-
Console.WriteLine($"Cannot find URL for '{heroName}'");
128+
Logger.OutputError($"Cannot find URL for '{heroName}'");
129129
return;
130130
}
131131

ArtifactAPI.MatchHistory/Logger.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace ArtifactAPI.MatchHistory
5+
{
6+
/// <summary>
7+
/// Simple logger class to track output messages like crashes and useful information
8+
/// </summary>
9+
public class Logger
10+
{
11+
private static string FILE_NAME = "output.txt";
12+
private static string FILE_PATH = null;
13+
14+
static bool IS_CONFIGURED = false;
15+
16+
public Logger()
17+
{
18+
Setup();
19+
}
20+
21+
public static void OutputInfo(string message)
22+
{
23+
if (!IS_CONFIGURED)
24+
Setup();
25+
26+
string formatted = $"{DateTime.Now} -- Info -- {message}{Environment.NewLine}";
27+
Append(formatted);
28+
}
29+
30+
public static void OutputError(string message)
31+
{
32+
if (!IS_CONFIGURED)
33+
Setup();
34+
35+
string formatted = $"{DateTime.Now} -- Error -- {message}{Environment.NewLine}";
36+
Append(formatted);
37+
}
38+
39+
private static void Append(string message)
40+
{
41+
File.AppendAllText(FILE_PATH, message);
42+
}
43+
44+
private static void Setup()
45+
{
46+
FILE_PATH = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILE_NAME);
47+
if (string.IsNullOrEmpty(FILE_PATH))
48+
return;
49+
50+
if (!File.Exists(FILE_PATH))
51+
{
52+
File.Create(FILE_PATH).Close();
53+
}
54+
55+
Append($"------{Environment.NewLine}");
56+
57+
IS_CONFIGURED = true;
58+
}
59+
}
60+
}

ArtifactAPI.MatchHistory/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:dtos="clr-namespace:ArtifactAPI.MatchHistory.Dtos"
77
xmlns:converters="clr-namespace:ArtifactAPI.MatchHistory.Converters"
88
mc:Ignorable="d"
9-
Title="ArtifactAPI - Artifact Match History v0.2"
9+
Title="ArtifactAPI - Artifact Match History"
1010
MinHeight="450" MinWidth="800"
1111
Height="500" Width="800"
1212
Icon="/Images/favicon.png" Background="#FF2E2E2E"

ArtifactAPI.MatchHistory/MainWindow.xaml.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ public partial class MainWindow : Window
1717
private ArtifactClient m_client = null;
1818
private List<Match> m_matches = null;
1919

20+
private static string VERSION = "v0.3";
21+
2022
public MainWindow()
2123
{
24+
Logger.OutputInfo($"Program Start - Artifact API {VERSION}");
2225
InitializeComponent();
2326

27+
this.Title += $" {VERSION}";
28+
Closed += OnProgramClosed;
29+
2430
Loaded += OnViewLoaded;
2531
outputBox.TextChanged += OnOutputPasted;
2632
tb_javascriptCopy.GotFocus += OnHaveGotFocus;
@@ -32,6 +38,11 @@ public MainWindow()
3238
m_client.GetAllCards();
3339
}
3440

41+
private void OnProgramClosed(object sender, EventArgs e)
42+
{
43+
Logger.OutputInfo("Program Closed");
44+
}
45+
3546
private void OnHaveGotFocus(object sender, RoutedEventArgs e)
3647
{
3748
tb_javascriptCopy.SelectAll();

ArtifactAPI.MatchHistory/MatchDecoder.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class MatchDecoder
1010
const char PROPERTY_SEPARATOR = '|';
1111
const char MATCH_SEPARATOR = ',';
1212

13+
public MatchDecoder()
14+
{
15+
}
16+
1317
public static List<Match> DecodeMatch(string s, ArtifactClient client)
1418
{
1519
if (string.IsNullOrEmpty(s))
@@ -18,10 +22,10 @@ public static List<Match> DecodeMatch(string s, ArtifactClient client)
1822
s = RemoveFormatting(s);
1923

2024
List<Match> matches = new List<Match>();
21-
try
25+
string[] matchArr = s.Split(MATCH_SEPARATOR);
26+
foreach (string match in matchArr)
2227
{
23-
string[] matchArr = s.Split(MATCH_SEPARATOR);
24-
foreach (string match in matchArr)
28+
try
2529
{
2630
if (string.IsNullOrEmpty(match))
2731
continue;
@@ -33,7 +37,7 @@ public static List<Match> DecodeMatch(string s, ArtifactClient client)
3337
{
3438
if (string.IsNullOrEmpty(properties[i]))
3539
{
36-
Console.WriteLine($"Empty property at value {i}");
40+
Logger.OutputError($"Empty property at value {i}");
3741
continue;
3842
}
3943

@@ -133,25 +137,25 @@ public static List<Match> DecodeMatch(string s, ArtifactClient client)
133137
}
134138
else
135139
{
136-
throw new NotImplementedException("Unknown property");
140+
throw new NotImplementedException($"Unknown property - '{properties[i]}'");
137141
}
138142
}
139143

140-
//Only add one instance of the game Id to the list.
141-
//Currently an issue with duplicate games in the history (Check ReadMe.md)
144+
///Only add one instance of the game Id to the list.
145+
///Currently an issue with duplicate games in the history (Check ReadMe.md)
142146
if (!matches.Exists(x => x.MatchId == m.MatchId))
143147
{
144148
matches.Add(m);
145149
}
146150
}
147-
148-
return matches;
149-
}
150-
catch (Exception e)
151-
{
152-
Console.WriteLine($"Unable to decode pasted content - '{e.ToString()}'");
153-
return null;
151+
catch (Exception e)
152+
{
153+
Logger.OutputError($"Unable to decode pasted content - '{e.ToString()}'");
154+
return null;
155+
}
154156
}
157+
158+
return matches;
155159
}
156160

157161
private static string RemoveFormatting(string s)

0 commit comments

Comments
 (0)