Skip to content

Commit 1c5b184

Browse files
authored
Merge pull request #140 from DrEsteban/PullRequest
Writing error logs to an ILogger instead of Console, adding an event for Unsupported Events
2 parents b8ae595 + bbe79fa commit 1c5b184

6 files changed

Lines changed: 3471 additions & 1637 deletions

File tree

obs-websocket-dotnet/Events.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using OBSWebsocketDotNet.Types.Events;
9+
using Microsoft.Extensions.Logging;
910

1011
namespace OBSWebsocketDotNet
1112
{
@@ -290,6 +291,11 @@ public partial class OBSWebsocket
290291
/// </summary>
291292
public event EventHandler<SceneNameChangedEventArgs> SceneNameChanged;
292293

294+
/// <summary>
295+
/// An unsupported event has been received.
296+
/// </summary>
297+
public event EventHandler<UnsupportedEventArgs> UnsupportedEvent;
298+
293299
#endregion
294300

295301
#region EventProcessing
@@ -521,8 +527,8 @@ protected void ProcessEventType(string eventType, JObject body)
521527

522528
default:
523529
var message = $"Unsupported Event: {eventType}\n{body}";
524-
Console.WriteLine(message);
525-
Debug.WriteLine(message);
530+
Logger?.LogInformation(message);
531+
UnsupportedEvent?.Invoke(this, new UnsupportedEventArgs(eventType, body));
526532
break;
527533
}
528534
}

obs-websocket-dotnet/OBSWebsocket.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
2+
using System.Collections.Concurrent;
3+
using System.Net.WebSockets;
24
using System.Security.Cryptography;
35
using System.Text;
4-
using Newtonsoft.Json.Linq;
56
using System.Threading.Tasks;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Logging.Abstractions;
69
using Newtonsoft.Json;
7-
using System.Collections.Concurrent;
8-
using System.Net.WebSockets;
9-
using Websocket.Client;
10+
using Newtonsoft.Json.Linq;
1011
using OBSWebsocketDotNet.Communication;
12+
using Websocket.Client;
1113

1214
namespace OBSWebsocketDotNet
1315
{
@@ -59,6 +61,11 @@ public bool IsConnected
5961
}
6062
}
6163

64+
/// <summary>
65+
/// Gets or sets the logger for this instance
66+
/// </summary>
67+
public ILogger<OBSWebsocket> Logger { get; set; } = NullLogger<OBSWebsocket>.Instance;
68+
6269
/// <summary>
6370
/// Constructor
6471
/// </summary>
@@ -193,6 +200,8 @@ private void WebsocketMessageHandler(object sender, ResponseMessage e)
193200
break;
194201
default:
195202
// Unsupported message type
203+
Logger?.LogWarning($"Unsupported message type: {msg.OperationCode}");
204+
UnsupportedEvent?.Invoke(this, new Types.Events.UnsupportedEventArgs(msg.OperationCode.ToString(), body));
196205
break;
197206

198207
}

obs-websocket-dotnet/OBSWebsocket_Requests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Microsoft.Extensions.Logging;
2+
using Newtonsoft.Json;
23
using Newtonsoft.Json.Linq;
34
using OBSWebsocketDotNet.Types;
45
using System;
@@ -339,7 +340,7 @@ public bool RemoveSourceFilter(string sourceName, string filterName)
339340
catch (Exception e)
340341
{
341342
//TODO exception handling
342-
Console.WriteLine(e.Message);
343+
Logger?.LogError(e, "Error removing filter");
343344
}
344345
return false;
345346
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace OBSWebsocketDotNet.Types.Events
5+
{
6+
/// <summary>
7+
/// Event args for unsupported events
8+
/// </summary>
9+
public class UnsupportedEventArgs : EventArgs
10+
{
11+
/// <summary>
12+
/// The type of the event
13+
/// </summary>
14+
public string EventType { get; }
15+
/// <summary>
16+
/// The body of the event
17+
/// </summary>
18+
public JObject Body { get; }
19+
20+
/// <summary>
21+
/// Event args for unsupported events
22+
/// </summary>
23+
public UnsupportedEventArgs(string eventType, JObject body)
24+
{
25+
EventType = eventType;
26+
Body = body;
27+
}
28+
}
29+
}

obs-websocket-dotnet/obs-websocket-dotnet.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ What's new in v5.0.0.3
2828
</PropertyGroup>
2929

3030
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
31-
<DocumentationFile>E:\Projects\GitHub\obs-websocket-dotnet-standard\obs-websocket-dotnet\obs-websocket-dotnet.xml</DocumentationFile>
31+
<DocumentationFile>obs-websocket-dotnet.xml</DocumentationFile>
3232
</PropertyGroup>
3333

3434
<ItemGroup>
35+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
3536
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
3637
<PackageReference Include="Websocket.Client" Version="4.4.43" />
3738
</ItemGroup>

0 commit comments

Comments
 (0)