Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing error logs to an ILogger instead of Console, adding an event for Unsupported Events #140

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 8 additions & 2 deletions obs-websocket-dotnet/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using OBSWebsocketDotNet.Types.Events;
using Microsoft.Extensions.Logging;

namespace OBSWebsocketDotNet
{
Expand Down Expand Up @@ -290,6 +291,11 @@ public partial class OBSWebsocket
/// </summary>
public event EventHandler<SceneNameChangedEventArgs> SceneNameChanged;

/// <summary>
/// An unsupported event has been received.
/// </summary>
public event EventHandler<UnsupportedEventArgs> UnsupportedEvent;

#endregion

#region EventProcessing
Expand Down Expand Up @@ -521,8 +527,8 @@ protected void ProcessEventType(string eventType, JObject body)

default:
var message = $"Unsupported Event: {eventType}\n{body}";
Console.WriteLine(message);
Debug.WriteLine(message);
Logger?.LogInformation(message);
UnsupportedEvent?.Invoke(this, new UnsupportedEventArgs(eventType, body));
break;
}
}
Expand Down
17 changes: 13 additions & 4 deletions obs-websocket-dotnet/OBSWebsocket.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Concurrent;
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Net.WebSockets;
using Websocket.Client;
using Newtonsoft.Json.Linq;
using OBSWebsocketDotNet.Communication;
using Websocket.Client;

namespace OBSWebsocketDotNet
{
Expand Down Expand Up @@ -59,6 +61,11 @@ public bool IsConnected
}
}

/// <summary>
/// Gets or sets the logger for this instance
/// </summary>
public ILogger<OBSWebsocket> Logger { get; set; } = NullLogger<OBSWebsocket>.Instance;

/// <summary>
/// Constructor
/// </summary>
Expand Down Expand Up @@ -193,6 +200,8 @@ private void WebsocketMessageHandler(object sender, ResponseMessage e)
break;
default:
// Unsupported message type
Logger?.LogWarning($"Unsupported message type: {msg.OperationCode}");
UnsupportedEvent?.Invoke(this, new Types.Events.UnsupportedEventArgs(msg.OperationCode.ToString(), body));
break;

}
Expand Down
5 changes: 3 additions & 2 deletions obs-websocket-dotnet/OBSWebsocket_Requests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OBSWebsocketDotNet.Types;
using System;
Expand Down Expand Up @@ -339,7 +340,7 @@ public bool RemoveSourceFilter(string sourceName, string filterName)
catch (Exception e)
{
//TODO exception handling
Console.WriteLine(e.Message);
Logger?.LogError(e, "Error removing filter");
}
return false;
}
Expand Down
29 changes: 29 additions & 0 deletions obs-websocket-dotnet/Types/Events/UnsupportedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Newtonsoft.Json.Linq;

namespace OBSWebsocketDotNet.Types.Events
{
/// <summary>
/// Event args for unsupported events
/// </summary>
public class UnsupportedEventArgs : EventArgs
{
/// <summary>
/// The type of the event
/// </summary>
public string EventType { get; }
/// <summary>
/// The body of the event
/// </summary>
public JObject Body { get; }

/// <summary>
/// Event args for unsupported events
/// </summary>
public UnsupportedEventArgs(string eventType, JObject body)
{
EventType = eventType;
Body = body;
}
}
}
3 changes: 2 additions & 1 deletion obs-websocket-dotnet/obs-websocket-dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ What's new in v5.0.0.3
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Websocket.Client" Version="4.4.43" />
</ItemGroup>
Expand Down
Loading