Skip to content

Commit 4b8e2c8

Browse files
committed
chore: update editor to use ILoggers
1 parent 971bcc7 commit 4b8e2c8

20 files changed

+206
-75
lines changed

Intersect.Editor/Content/ContentManager.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Intersect.GameObjects;
55
using Intersect.IO.Files;
66
using Intersect.Utilities;
7+
using Microsoft.Extensions.Logging;
78
using Microsoft.Xna.Framework;
89
using Microsoft.Xna.Framework.Content;
910
using Microsoft.Xna.Framework.Graphics;
@@ -220,14 +221,19 @@ public static void LoadTilesets()
220221
}
221222
catch (Exception exception)
222223
{
223-
ApplicationContext.Context.Value?.Logger.LogError($"Fake methods! ({tileset.Name})");
224+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
225+
exception,
226+
$"Fake methods! ({tileset.Name})"
227+
);
228+
224229
if (exception.InnerException != null)
225230
{
226-
ApplicationContext.Context.Value?.Logger.LogError(exception.InnerException);
231+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
232+
exception.InnerException,
233+
"Caused by"
234+
);
227235
}
228236

229-
ApplicationContext.Context.Value?.Logger.LogError(exception);
230-
231237
throw;
232238
}
233239

@@ -467,7 +473,7 @@ public static Effect GetShader(string name)
467473
{
468474
if (string.IsNullOrWhiteSpace(name))
469475
{
470-
ApplicationContext.Context.Value?.Logger.LogError("Tried to load shader with null name.");
476+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError("Tried to load shader with null name.");
471477

472478
return null;
473479
}
@@ -484,7 +490,7 @@ public static object GetMusic(string name)
484490
{
485491
if (string.IsNullOrWhiteSpace(name))
486492
{
487-
ApplicationContext.Context.Value?.Logger.LogError("Tried to load music with null name.");
493+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError("Tried to load music with null name.");
488494

489495
return null;
490496
}
@@ -501,7 +507,7 @@ public static object GetSound(string name)
501507
{
502508
if (string.IsNullOrWhiteSpace(name))
503509
{
504-
ApplicationContext.Context.Value?.Logger.LogError("Tried to load sound with null name.");
510+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError("Tried to load sound with null name.");
505511

506512
return null;
507513
}

Intersect.Editor/Content/Texture.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Intersect.IO.Files;
22
using Intersect.Utilities;
3+
using Microsoft.Extensions.Logging;
34
using Microsoft.Xna.Framework.Graphics;
45
using Graphics = Intersect.Editor.Core.Graphics;
56

@@ -30,7 +31,7 @@ public void LoadTexture()
3031
mLoadError = true;
3132
if (string.IsNullOrWhiteSpace(mPath))
3233
{
33-
ApplicationContext.Context.Value?.Logger.LogError("Invalid texture path (empty/null).");
34+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError("Invalid texture path (empty/null).");
3435

3536
return;
3637
}
@@ -39,7 +40,7 @@ public void LoadTexture()
3940

4041
if (!File.Exists(mPath))
4142
{
42-
ApplicationContext.Context.Value?.Logger.LogError($"Texture does not exist: {relativePath}");
43+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError($"Texture does not exist: {relativePath}");
4344

4445
return;
4546
}
@@ -51,7 +52,7 @@ public void LoadTexture()
5152
mTexture = Texture2D.FromStream(Graphics.GetGraphicsDevice(), fileStream);
5253
if (mTexture == null)
5354
{
54-
ApplicationContext.Context.Value?.Logger.LogError($"Failed to load texture due to unknown error: {relativePath}");
55+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError($"Failed to load texture due to unknown error: {relativePath}");
5556

5657
return;
5758
}
@@ -62,7 +63,7 @@ public void LoadTexture()
6263
}
6364
catch (Exception exception)
6465
{
65-
ApplicationContext.Context.Value?.Logger.LogError(
66+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
6667
exception,
6768
$"Failed to load texture ({FileSystemHelper.FormatSize(fileStream.Length)}): {relativePath}"
6869
);
@@ -135,7 +136,7 @@ public void GetDimensions()
135136
mLoadError = true;
136137
if (string.IsNullOrWhiteSpace(mPath))
137138
{
138-
ApplicationContext.Context.Value?.Logger.LogError("Invalid texture path (empty/null).");
139+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError("Invalid texture path (empty/null).");
139140

140141
return;
141142
}
@@ -144,7 +145,7 @@ public void GetDimensions()
144145

145146
if (!File.Exists(mPath))
146147
{
147-
ApplicationContext.Context.Value?.Logger.LogError($"Texture does not exist: {relativePath}");
148+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError($"Texture does not exist: {relativePath}");
148149

149150
return;
150151
}
@@ -156,7 +157,7 @@ public void GetDimensions()
156157
var img = System.Drawing.Image.FromStream(fileStream, false, false);
157158
if (img == null)
158159
{
159-
ApplicationContext.Context.Value?.Logger.LogError($"Failed to load texture due to unknown error: {relativePath}");
160+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError($"Failed to load texture due to unknown error: {relativePath}");
160161

161162
return;
162163
}
@@ -167,7 +168,7 @@ public void GetDimensions()
167168
}
168169
catch (Exception exception)
169170
{
170-
ApplicationContext.Context.Value?.Logger.LogError(
171+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
171172
exception,
172173
$"Failed to load texture ({FileSystemHelper.FormatSize(fileStream.Length)}): {relativePath}"
173174
);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Intersect.Core;
2+
using Intersect.Plugins.Interfaces;
3+
using Intersect.Threading;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Intersect.Editor.Core;
7+
8+
public sealed class FakeApplicationContextForThisGarbageWinFormsEditorThatIHateAndWishItWouldBurnInAFireContext : IApplicationContext<FakeStartupOptionsThatDoNotEverGetUsedForThisGarbageWinFormsEditorStartupOptions>
9+
{
10+
public void Dispose()
11+
{
12+
IsDisposed = true;
13+
}
14+
15+
public bool HasErrors { get; set; }
16+
public bool IsDisposed { get; private set; }
17+
public bool IsStarted { get; set; }
18+
public bool IsRunning { get; set; }
19+
ICommandLineOptions IApplicationContext.StartupOptions => StartupOptions;
20+
21+
public FakeStartupOptionsThatDoNotEverGetUsedForThisGarbageWinFormsEditorStartupOptions StartupOptions { get; }
22+
public ILogger Logger { get; }
23+
public IPacketHelper PacketHelper => throw new NotSupportedException();
24+
public List<IApplicationService> Services { get; }
25+
26+
public TApplicationService GetService<TApplicationService>() where TApplicationService : IApplicationService
27+
{
28+
throw new NotImplementedException();
29+
}
30+
31+
public void Start(bool lockUntilShutdown = true)
32+
{
33+
throw new NotImplementedException();
34+
}
35+
36+
public ILockingActionQueue StartWithActionQueue()
37+
{
38+
throw new NotImplementedException();
39+
}
40+
41+
public FakeApplicationContextForThisGarbageWinFormsEditorThatIHateAndWishItWouldBurnInAFireContext(ILogger logger)
42+
{
43+
Logger = logger;
44+
StartupOptions = new FakeStartupOptionsThatDoNotEverGetUsedForThisGarbageWinFormsEditorStartupOptions();
45+
Services = [];
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Intersect.Core;
2+
3+
namespace Intersect.Editor.Core;
4+
5+
public sealed class
6+
FakeStartupOptionsThatDoNotEverGetUsedForThisGarbageWinFormsEditorStartupOptions : ICommandLineOptions
7+
{
8+
public string WorkingDirectory => Environment.CurrentDirectory;
9+
public IEnumerable<string> PluginDirectories => [];
10+
}

Intersect.Editor/Core/Graphics.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Intersect.GameObjects;
1111
using Intersect.GameObjects.Maps;
1212
using Intersect.Utilities;
13+
using Microsoft.Extensions.Logging;
1314
using Microsoft.Xna.Framework;
1415
using Microsoft.Xna.Framework.Graphics;
1516

@@ -215,11 +216,10 @@ public static void Render()
215216
}
216217
catch (Exception exception)
217218
{
218-
ApplicationContext.Context.Value?.Logger.LogError(
219+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
220+
exception,
219221
$"{Globals.MapGrid.Grid.GetLength(0)}x{Globals.MapGrid.Grid.GetLength(1)} -- {x},{y}"
220222
);
221-
222-
ApplicationContext.Context.Value?.Logger.LogError(exception);
223223
}
224224

225225
if (map != null)
@@ -757,8 +757,10 @@ RenderTarget2D renderTarget2D
757757
}
758758
catch (Exception exception)
759759
{
760-
ApplicationContext.Context.Value?.Logger.LogError($"map={tmpMap != null},layer{drawLayer}.tiles={tmpMap.Layers[drawLayer] != null}");
761-
ApplicationContext.Context.Value?.Logger.LogError(exception);
760+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
761+
exception,
762+
$"map={tmpMap != null},layer{drawLayer}.tiles={tmpMap.Layers[drawLayer] != null}"
763+
);
762764

763765
continue;
764766
}

Intersect.Editor/Core/Main.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Intersect.Editor.Localization;
55
using Intersect.Editor.Maps;
66
using Intersect.Utilities;
7+
using Microsoft.Extensions.Logging;
78

89
namespace Intersect.Editor.Core;
910

@@ -154,10 +155,11 @@ private static void UpdateMaps()
154155
Application.DoEvents();
155156
}
156157
}
157-
catch (Exception ex)
158+
catch (Exception exception)
158159
{
159-
ApplicationContext.Context.Value?.Logger.LogError(
160-
ex, "JC's Solution for UpdateMaps collection was modified bug did not work!"
160+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
161+
exception,
162+
"JC's Solution for UpdateMaps collection was modified bug did not work!"
161163
);
162164
}
163165

Intersect.Editor/Core/Program.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
using System.Reflection;
33
using Intersect.Editor.Forms;
44
using Intersect.Editor.General;
5+
using Microsoft.Extensions.Logging;
6+
using Serilog;
7+
using Serilog.Events;
8+
using Serilog.Extensions.Logging;
9+
using ApplicationContext = Intersect.Core.ApplicationContext;
510

611

712
namespace Intersect.Editor.Core;
@@ -14,6 +19,25 @@ public static partial class Program
1419

1520
static Program()
1621
{
22+
var executableName = Path.GetFileNameWithoutExtension(
23+
Process.GetCurrentProcess().MainModule?.FileName ?? Assembly.GetExecutingAssembly().GetName().Name
24+
);
25+
var loggerConfiguration = new LoggerConfiguration().MinimumLevel
26+
.Is(Debugger.IsAttached ? LogEventLevel.Debug : LogEventLevel.Information).Enrich.FromLogContext().WriteTo
27+
.Console().WriteTo.File(
28+
Path.Combine(
29+
"logs",
30+
$"{executableName}-{Process.GetCurrentProcess().StartTime:yyyy_MM_dd-HH_mm_ss_fff}.log"
31+
)
32+
).WriteTo.File(
33+
Path.Combine("logs", $"errors-{executableName}.log"),
34+
restrictedToMinimumLevel: LogEventLevel.Error
35+
);
36+
37+
var logger = new SerilogLoggerFactory(loggerConfiguration.CreateLogger()).CreateLogger("Client");
38+
ApplicationContext.Context.Value =
39+
new FakeApplicationContextForThisGarbageWinFormsEditorThatIHateAndWishItWouldBurnInAFireContext(logger);
40+
1741
var iconStream = typeof(Program).Assembly.GetManifestResourceStream(IconManifestResourceName);
1842
if (iconStream == default)
1943
{
@@ -32,15 +56,15 @@ static Program()
3256
[STAThread]
3357
public static void Main()
3458
{
35-
ApplicationContext.Context.Value?.Logger.LogTrace("Starting editor...");
59+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Starting editor...");
3660

3761
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
3862
Application.ThreadException += Application_ThreadException;
3963
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
4064
Application.EnableVisualStyles();
4165
Application.SetCompatibleTextRenderingDefault(false);
4266

43-
ApplicationContext.Context.Value?.Logger.LogTrace("Unpacking libraries...");
67+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Unpacking libraries...");
4468

4569
//Place sqlite3.dll where it's needed.
4670
var dllname = Environment.Is64BitProcess ? "sqlite3x64.dll" : "sqlite3x86.dll";
@@ -56,15 +80,15 @@ public static void Main()
5680
}
5781
}
5882

59-
ApplicationContext.Context.Value?.Logger.LogTrace("Libraries unpacked.");
83+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Libraries unpacked.");
6084

61-
ApplicationContext.Context.Value?.Logger.LogTrace("Creating forms...");
85+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Creating forms...");
6286
Globals.UpdateForm = new FrmUpdate();
6387
Globals.LoginForm = new FrmLogin();
6488
Globals.MainForm = new FrmMain();
65-
ApplicationContext.Context.Value?.Logger.LogTrace("Forms created.");
89+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Forms created.");
6690

67-
ApplicationContext.Context.Value?.Logger.LogTrace("Starting application.");
91+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogTrace("Starting application.");
6892
Application.Run(Globals.UpdateForm);
6993
}
7094

@@ -76,7 +100,10 @@ private static void Application_ThreadException(object sender, ThreadExceptionEv
76100
//Really basic error handler for debugging purposes
77101
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs exception)
78102
{
79-
ApplicationContext.Context.Value?.Logger.LogError(Exception) exception?.ExceptionObject);
103+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogError(
104+
(Exception)exception?.ExceptionObject,
105+
"Unhandled exception"
106+
);
80107
MessageBox.Show(
81108
@"The Intersect Editor has encountered an error and must close. Error information can be found in logs/errors.log"
82109
);

Intersect.Editor/Forms/Controls/MapTreeList.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Runtime.InteropServices;
22
using Intersect.Editor.Networking;
33
using Intersect.GameObjects.Maps.MapList;
4+
using Microsoft.Extensions.Logging;
45

56

67
namespace Intersect.Editor.Forms.Controls;
@@ -257,7 +258,7 @@ private void BeginEdit(TreeNode node)
257258

258259
public void UpdateMapList(Guid selectMapId = default, List<Guid>? restrictMaps = null)
259260
{
260-
ApplicationContext.Context.Value?.Logger.LogInformation("Updating list");
261+
Intersect.Core.ApplicationContext.Context.Value?.Logger.LogInformation("Updating list");
261262
var selectedMapListMap = selectMapId == default ? default : MapList.List.FindMap(selectMapId);
262263
if (selectedMapListMap != default && _nodeLookup.TryGetValue(selectedMapListMap, out var treeNode))
263264
{

0 commit comments

Comments
 (0)