Skip to content

Commit 49e6d99

Browse files
committed
Replace serilog with Microsoft logging
Set the logging to off by default Can enable/disable logging in the global settings menu
1 parent a7aa703 commit 49e6d99

18 files changed

Lines changed: 360 additions & 122 deletions

File tree

YMouseButtonControl.Core/Services/KeyboardAndMouse/Implementations/EventSimulatorService.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading;
4-
using Serilog;
4+
using Microsoft.Extensions.Logging;
55
using SharpHook;
66
using SharpHook.Native;
7+
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
78

89
namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;
910

@@ -34,10 +35,11 @@ public interface IEventSimulatorService
3435
void TapKeys(string? keys, int delay, CancellationToken cancellationToken);
3536
}
3637

37-
public class EventSimulatorService(IEventSimulator eventSimulator) : IEventSimulatorService
38+
public partial class EventSimulatorService(
39+
ILogger<EventSimulatorService> logger,
40+
IEventSimulator eventSimulator
41+
) : IEventSimulatorService
3842
{
39-
private readonly ILogger _logger = Log.Logger.ForContext<EventSimulatorService>();
40-
4143
public void SimulateMousePress(MouseButton mb)
4244
{
4345
var t = new Thread(() => eventSimulator.SimulateMousePress(mb));
@@ -52,13 +54,13 @@ public void SimulateMouseRelease(MouseButton mb)
5254

5355
public void SimulateKeyPress(string? key)
5456
{
55-
_logger.Information("Simulate press {Key}", key);
57+
LogSimulateKeyPress(logger, key);
5658
eventSimulator.SimulateKeyPress(KeyCodes[key ?? throw new NullReferenceException(key)]);
5759
}
5860

5961
public void SimulateKeyRelease(string? key)
6062
{
61-
_logger.Information("Simulate release {Key}", key);
63+
LogSimulateKeyRelease(logger, key);
6264
eventSimulator.SimulateKeyRelease(KeyCodes[key ?? throw new NullReferenceException(key)]);
6365
}
6466

@@ -69,7 +71,7 @@ public void SimulateKeyRelease(string? key)
6971
/// <returns></returns>
7072
public void SimulateKeyTap(string? key)
7173
{
72-
_logger.Information("Simulate key tap {Key}", key);
74+
LogSimulateKeyTap(logger, key);
7375
var keyCode = KeyCodes[key ?? throw new NullReferenceException(key)];
7476
eventSimulator.SimulateKeyPress(keyCode);
7577
eventSimulator.SimulateKeyRelease(keyCode);
@@ -198,7 +200,7 @@ public void TapKeys(string? keys, int delay, CancellationToken cancellationToken
198200
{
199201
if (cancellationToken.IsCancellationRequested)
200202
{
201-
_logger.Information("========STOPPING TAP KEYS===========");
203+
LogStopTappingKeys(logger);
202204
return;
203205
}
204206
if (delay > -1)
@@ -244,7 +246,7 @@ public void TapKeys(string? keys, int delay, CancellationToken cancellationToken
244246
{
245247
if (cancellationToken.IsCancellationRequested)
246248
{
247-
_logger.Information("========STOPPING TAP KEYS===========");
249+
LogStopTappingKeys(logger);
248250
return;
249251
}
250252
switch (poppedPk.Value)
@@ -520,6 +522,18 @@ private static List<ParsedKey> ParseKeys(string? keys)
520522
{ "mb4", MouseButton.Button4 },
521523
{ "mb5", MouseButton.Button5 },
522524
};
525+
526+
[LoggerMessage(LogLevel.Information, "========STOPPING TAP KEYS===========")]
527+
private static partial void LogStopTappingKeys(ILogger logger);
528+
529+
[LoggerMessage(LogLevel.Information, "Simulate key tap {Key}")]
530+
private static partial void LogSimulateKeyTap(ILogger logger, string? key);
531+
532+
[LoggerMessage(LogLevel.Information, "Simulate press {Key}")]
533+
private static partial void LogSimulateKeyPress(ILogger logger, string? key);
534+
535+
[LoggerMessage(LogLevel.Information, "Simulate release {Key}")]
536+
private static partial void LogSimulateKeyRelease(ILogger logger, string? key);
523537
}
524538

525539
internal class ParsedKey

YMouseButtonControl.Core/Services/KeyboardAndMouse/Implementations/MouseListenerService.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
using System.Reactive.Linq;
44
using System.Reactive.Subjects;
55
using System.Threading;
6-
using Serilog;
6+
using Microsoft.Extensions.Logging;
77
using SharpHook;
88
using SharpHook.Native;
99
using SharpHook.Reactive;
1010
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
1111
using YMouseButtonControl.Core.Services.KeyboardAndMouse.EventArgs;
1212
using YMouseButtonControl.Core.Services.Processes;
1313
using YMouseButtonControl.Core.Services.Profiles;
14+
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
1415

1516
namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;
1617

@@ -27,12 +28,12 @@ public interface IMouseListener : IDisposable
2728
/// Wrapper around sharphook for listening to mouse events
2829
/// Converts mouse events to NewMouseHookEventArgs
2930
/// </summary>
30-
public class MouseListener : IMouseListener
31+
public partial class MouseListener : IMouseListener
3132
{
33+
private readonly ILogger<MouseListener> _logger;
3234
private readonly IReactiveGlobalHook _hook;
3335
private readonly IProfilesService _profilesService;
3436
private readonly ICurrentWindowService _currentWindowService;
35-
private readonly ILogger _log = Log.Logger.ForContext<MouseListener>();
3637
private Thread? _thread;
3738
private readonly IDisposable? _mouseMovedDisposable;
3839
private readonly IDisposable? _mousePressedDisposable;
@@ -44,11 +45,13 @@ public class MouseListener : IMouseListener
4445
private readonly Subject<NewMouseWheelEventArgs> _mouseWheelSubject;
4546

4647
public MouseListener(
48+
ILogger<MouseListener> logger,
4749
IReactiveGlobalHook hook,
4850
IProfilesService profilesService,
4951
ICurrentWindowService currentWindowService
5052
)
5153
{
54+
_logger = logger;
5255
_hook = hook;
5356
_profilesService = profilesService;
5457
_currentWindowService = currentWindowService;
@@ -78,7 +81,7 @@ public void Run()
7881
{
7982
_thread = new Thread(() =>
8083
{
81-
_log.Information("Starting mouse listener");
84+
LogStartup(_logger);
8285
_hook.Run();
8386
});
8487
_thread.Start();
@@ -175,8 +178,9 @@ private void ConvertMouseReleasedEvent(MouseHookEventArgs e)
175178
{
176179
return;
177180
}
178-
_log.Information("Translate release {Button}", e.Data.Button);
179-
_log.Information("ACTIVE WINDOW {Foreground}", _currentWindowService.ForegroundWindow);
181+
182+
LogTranslateRelease(_logger, e.Data.Button);
183+
LogActiveWindow(_logger, _currentWindowService.ForegroundWindow);
180184
var args = new NewMouseHookEventArgs(
181185
(YMouseButton)e.Data.Button,
182186
e.Data.X,
@@ -185,12 +189,12 @@ private void ConvertMouseReleasedEvent(MouseHookEventArgs e)
185189
);
186190
if (ShouldSuppressEvent(args))
187191
{
188-
_log.Information("Suppressing {Button}: Release", e.Data.Button);
192+
LogSuppressingButtonRelease(_logger, e.Data.Button);
189193
e.SuppressEvent = true;
190194
}
191195
else
192196
{
193-
_log.Information("Not suppressing {Button}: Release", e.Data.Button);
197+
LogNotSuppressingButtonRelease(_logger, e.Data.Button);
194198
}
195199
_mouseReleasedSubject.OnNext(args);
196200
}
@@ -201,8 +205,8 @@ private void ConvertMousePressedEvent(MouseHookEventArgs e)
201205
{
202206
return;
203207
}
204-
_log.Information("Translate press {Button}", e.Data.Button);
205-
_log.Information("ACTIVE WINDOW {Foreground}", _currentWindowService.ForegroundWindow);
208+
LogTranslateButton(_logger, e.Data.Button);
209+
LogActiveWindow(_logger, _currentWindowService.ForegroundWindow);
206210

207211
var args = new NewMouseHookEventArgs(
208212
(YMouseButton)e.Data.Button,
@@ -212,12 +216,12 @@ private void ConvertMousePressedEvent(MouseHookEventArgs e)
212216
);
213217
if (ShouldSuppressEvent(args))
214218
{
215-
_log.Information("Suppressing {Button}: Press", e.Data.Button);
219+
LogSuppressingButtonRelease(_logger, e.Data.Button);
216220
e.SuppressEvent = true;
217221
}
218222
else
219223
{
220-
_log.Information("Not suppressing {Button}: Press", e.Data.Button);
224+
LogNotSuppressingButtonRelease(_logger, e.Data.Button);
221225
}
222226
_mousePressedSubject.OnNext(args);
223227
}
@@ -242,4 +246,28 @@ public void Dispose()
242246

243247
_thread?.Join();
244248
}
249+
250+
[LoggerMessage(LogLevel.Information, "Translate press {Button}")]
251+
private static partial void LogTranslateButton(ILogger logger, MouseButton button);
252+
253+
[LoggerMessage(LogLevel.Information, "Suppressing {Button}: Press")]
254+
private static partial void LogSuppressingButtonPress(ILogger logger, MouseButton button);
255+
256+
[LoggerMessage(LogLevel.Information, "Not suppressing {Button}: Press")]
257+
private static partial void LogNotSuppressingButtonPress(ILogger logger, MouseButton button);
258+
259+
[LoggerMessage(LogLevel.Information, "Not suppressing {Button}: Release")]
260+
public static partial void LogNotSuppressingButtonRelease(ILogger logger, MouseButton button);
261+
262+
[LoggerMessage(LogLevel.Information, "Suppressing {Button}: Release")]
263+
public static partial void LogSuppressingButtonRelease(ILogger logger, MouseButton button);
264+
265+
[LoggerMessage(LogLevel.Information, "ACTIVE WINDOW {Foreground}")]
266+
private static partial void LogActiveWindow(ILogger logger, string foreground);
267+
268+
[LoggerMessage(LogLevel.Information, "Translate release {Button}")]
269+
private static partial void LogTranslateRelease(ILogger logger, MouseButton button);
270+
271+
[LoggerMessage(LogLevel.Information, "Starting mouse listener")]
272+
private static partial void LogStartup(ILogger logger);
245273
}

YMouseButtonControl.Core/Services/KeyboardAndMouse/Implementations/SimulatedKeystrokesTypes/StickyRepeatService.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Threading;
2-
using Serilog;
2+
using Microsoft.Extensions.Logging;
33
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
4-
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Interfaces;
54
using YMouseButtonControl.Core.ViewModels.Models;
65

76
namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations.SimulatedKeystrokesTypes;
@@ -11,15 +10,16 @@ public interface IStickyRepeatService
1110
void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state);
1211
}
1312

14-
public class StickyRepeatService(IEventSimulatorService eventSimulatorService)
15-
: IStickyRepeatService
13+
public partial class StickyRepeatService(
14+
ILogger<StickyRepeatService> logger,
15+
IEventSimulatorService eventSimulatorService
16+
) : IStickyRepeatService
1617
{
1718
private Thread? _thread;
1819
private bool _shouldStop;
1920
private readonly object _lock = new();
2021
private const int RepeatRateMs = 33;
2122
private CancellationTokenSource? _cts;
22-
private readonly ILogger _log = Log.Logger.ForContext<StickyRepeatService>();
2323

2424
public void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state)
2525
{
@@ -36,7 +36,7 @@ public void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state)
3636
{
3737
lock (_lock)
3838
{
39-
_log.Information("=====CANCELLATION REQUESTED=======");
39+
LogCancellationRequested(logger);
4040
_cts?.Cancel();
4141
_shouldStop = true;
4242
}
@@ -63,7 +63,7 @@ private void StartThread(BaseButtonMappingVm mapping)
6363
{
6464
if (_shouldStop)
6565
{
66-
_log.Information("=====CANCELLATION REQUESTED=======");
66+
LogCancellationRequested(logger);
6767
_cts.Cancel();
6868
break;
6969
}
@@ -75,4 +75,7 @@ private void StartThread(BaseButtonMappingVm mapping)
7575

7676
_thread.Start();
7777
}
78+
79+
[LoggerMessage(LogLevel.Information, "=====CANCELLATION REQUESTED=======")]
80+
private static partial void LogCancellationRequested(ILogger logger);
7881
}

YMouseButtonControl.Core/Services/KeyboardAndMouse/KeyboardSimulatorWorker.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using Serilog;
2+
using Microsoft.Extensions.Logging;
33
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
44
using YMouseButtonControl.Core.Services.KeyboardAndMouse.EventArgs;
55
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;
@@ -11,7 +11,8 @@
1111

1212
namespace YMouseButtonControl.Core.Services.KeyboardAndMouse;
1313

14-
public class KeyboardSimulatorWorker(
14+
public partial class KeyboardSimulatorWorker(
15+
ILogger<KeyboardSimulatorWorker> logger,
1516
IProfilesService profilesService,
1617
IMouseListener mouseListener,
1718
ISkipProfileService skipProfileService,
@@ -24,7 +25,6 @@ public class KeyboardSimulatorWorker(
2425
IRightClick rightClick
2526
) : IDisposable
2627
{
27-
private readonly ILogger _log = Log.Logger.ForContext<KeyboardSimulatorWorker>();
2828
private IDisposable? _onMousePressedDisposable;
2929
private IDisposable? _onMouseReleasedDisposable;
3030
private IDisposable? _onMouseWheelDisposable;
@@ -51,11 +51,10 @@ private void OnMousePressed(NewMouseHookEventArgs e)
5151
{
5252
if (skipProfileService.ShouldSkipProfile(p, e))
5353
{
54-
_log.Information("Skipped {Profile}", p.Name);
54+
LogSkippedProfile(logger, p.Name);
5555
continue;
5656
}
57-
58-
_log.Information("{Profile}, Route {Button}", p.Name, e.Button);
57+
LogMousePressedRoute(logger, p.Name, e.Button);
5958
RouteMouseButton(e.Button, p, MouseButtonState.Pressed);
6059
}
6160
}
@@ -151,4 +150,14 @@ MouseButtonState state
151150
}
152151

153152
private void OnMouseWheel(NewMouseWheelEventArgs e) { }
153+
154+
[LoggerMessage(LogLevel.Information, "Skipped {Profile}")]
155+
private static partial void LogSkippedProfile(ILogger logger, string profile);
156+
157+
[LoggerMessage(LogLevel.Information, "{Profile}, Route {Button}")]
158+
private static partial void LogMousePressedRoute(
159+
ILogger logger,
160+
string profile,
161+
YMouseButton button
162+
);
154163
}

0 commit comments

Comments
 (0)