From f5ce355747d95cd5f4e814d6cce5251776fb4591 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Tue, 3 Feb 2026 14:12:39 -0500 Subject: [PATCH] Fix logging levels - use appropriate debug/info/warn/error levels --- .../Middleware/WebSocketProxyMiddleware.cs | 28 ++++++------ .../Jellyfin/JellyfinSessionManager.cs | 44 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/allstarr/Middleware/WebSocketProxyMiddleware.cs b/allstarr/Middleware/WebSocketProxyMiddleware.cs index d3a9cfc..064a607 100644 --- a/allstarr/Middleware/WebSocketProxyMiddleware.cs +++ b/allstarr/Middleware/WebSocketProxyMiddleware.cs @@ -23,7 +23,7 @@ public class WebSocketProxyMiddleware _settings = settings.Value; _logger = logger; - _logger.LogWarning("πŸ”§ WEBSOCKET: WebSocketProxyMiddleware initialized - Jellyfin URL: {Url}", _settings.Url); + _logger.LogDebug("πŸ”§ WEBSOCKET: WebSocketProxyMiddleware initialized - Jellyfin URL: {Url}", _settings.Url); } public async Task InvokeAsync(HttpContext context) @@ -38,7 +38,7 @@ public class WebSocketProxyMiddleware isWebSocket || context.Request.Headers.ContainsKey("Upgrade")) { - _logger.LogWarning("πŸ” WEBSOCKET: Potential WebSocket request: Path={Path}, IsWs={IsWs}, Method={Method}, Upgrade={Upgrade}, Connection={Connection}", + _logger.LogDebug("πŸ” WEBSOCKET: Potential WebSocket request: Path={Path}, IsWs={IsWs}, Method={Method}, Upgrade={Upgrade}, Connection={Connection}", path, isWebSocket, context.Request.Method, @@ -50,7 +50,7 @@ public class WebSocketProxyMiddleware if (context.Request.Path.StartsWithSegments("/socket", StringComparison.OrdinalIgnoreCase) && context.WebSockets.IsWebSocketRequest) { - _logger.LogWarning("πŸ”Œ WEBSOCKET: WebSocket connection request received from {RemoteIp}", + _logger.LogInformation("πŸ”Œ WEBSOCKET: WebSocket connection request received from {RemoteIp}", context.Connection.RemoteIpAddress); await HandleWebSocketProxyAsync(context); @@ -70,7 +70,7 @@ public class WebSocketProxyMiddleware { // Accept the WebSocket connection from the client clientWebSocket = await context.WebSockets.AcceptWebSocketAsync(); - _logger.LogWarning("βœ“ WEBSOCKET: Client WebSocket accepted"); + _logger.LogDebug("βœ“ WEBSOCKET: Client WebSocket accepted"); // Build Jellyfin WebSocket URL var jellyfinUrl = _settings.Url?.TrimEnd('/') ?? ""; @@ -84,7 +84,7 @@ public class WebSocketProxyMiddleware jellyfinWsUrl += context.Request.QueryString.Value; } - _logger.LogWarning("πŸ”— WEBSOCKET: Connecting to Jellyfin WebSocket: {Url}", jellyfinWsUrl); + _logger.LogDebug("πŸ”— WEBSOCKET: Connecting to Jellyfin WebSocket: {Url}", jellyfinWsUrl); // Connect to Jellyfin WebSocket serverWebSocket = new ClientWebSocket(); @@ -94,7 +94,7 @@ public class WebSocketProxyMiddleware if (context.Request.Headers.TryGetValue("X-Emby-Authorization", out var embyAuthHeader)) { serverWebSocket.Options.SetRequestHeader("X-Emby-Authorization", embyAuthHeader.ToString()); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Forwarded X-Emby-Authorization header"); + _logger.LogDebug("πŸ”‘ WEBSOCKET: Forwarded X-Emby-Authorization header"); } else if (context.Request.Headers.TryGetValue("Authorization", out var authHeader)) { @@ -103,12 +103,12 @@ public class WebSocketProxyMiddleware if (authValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase)) { serverWebSocket.Options.SetRequestHeader("X-Emby-Authorization", authValue); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Converted Authorization to X-Emby-Authorization header"); + _logger.LogDebug("πŸ”‘ WEBSOCKET: Converted Authorization to X-Emby-Authorization header"); } else { serverWebSocket.Options.SetRequestHeader("Authorization", authValue); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Forwarded Authorization header"); + _logger.LogDebug("πŸ”‘ WEBSOCKET: Forwarded Authorization header"); } } @@ -116,7 +116,7 @@ public class WebSocketProxyMiddleware serverWebSocket.Options.SetRequestHeader("User-Agent", "Allstarr/1.0"); await serverWebSocket.ConnectAsync(new Uri(jellyfinWsUrl), context.RequestAborted); - _logger.LogWarning("βœ“ WEBSOCKET: Connected to Jellyfin WebSocket"); + _logger.LogInformation("βœ“ WEBSOCKET: Connected to Jellyfin WebSocket"); // Start bidirectional proxying var clientToServer = ProxyMessagesAsync(clientWebSocket, serverWebSocket, "Clientβ†’Server", context.RequestAborted); @@ -125,7 +125,7 @@ public class WebSocketProxyMiddleware // Wait for either direction to complete await Task.WhenAny(clientToServer, serverToClient); - _logger.LogWarning("πŸ”Œ WEBSOCKET: WebSocket proxy connection closed"); + _logger.LogDebug("πŸ”Œ WEBSOCKET: WebSocket proxy connection closed"); } catch (WebSocketException wsEx) { @@ -165,7 +165,7 @@ public class WebSocketProxyMiddleware clientWebSocket?.Dispose(); serverWebSocket?.Dispose(); - _logger.LogWarning("🧹 WEBSOCKET: WebSocket connections cleaned up"); + _logger.LogDebug("🧹 WEBSOCKET: WebSocket connections cleaned up"); } } @@ -186,7 +186,7 @@ public class WebSocketProxyMiddleware if (result.MessageType == WebSocketMessageType.Close) { - _logger.LogWarning("πŸ”Œ WEBSOCKET {Direction}: Close message received", direction); + _logger.LogDebug("πŸ”Œ WEBSOCKET {Direction}: Close message received", direction); await destination.CloseAsync( result.CloseStatus ?? WebSocketCloseStatus.NormalClosure, result.CloseStatusDescription, @@ -226,11 +226,11 @@ public class WebSocketProxyMiddleware } catch (OperationCanceledException) { - _logger.LogWarning("⚠️ WEBSOCKET {Direction}: Operation cancelled", direction); + _logger.LogDebug("⚠️ WEBSOCKET {Direction}: Operation cancelled", direction); } catch (WebSocketException wsEx) when (wsEx.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { - _logger.LogWarning("⚠️ WEBSOCKET {Direction}: Connection closed prematurely", direction); + _logger.LogDebug("⚠️ WEBSOCKET {Direction}: Connection closed prematurely", direction); } catch (Exception ex) { diff --git a/allstarr/Services/Jellyfin/JellyfinSessionManager.cs b/allstarr/Services/Jellyfin/JellyfinSessionManager.cs index 9e49bd5..74b66d6 100644 --- a/allstarr/Services/Jellyfin/JellyfinSessionManager.cs +++ b/allstarr/Services/Jellyfin/JellyfinSessionManager.cs @@ -33,7 +33,7 @@ public class JellyfinSessionManager : IDisposable // Keep sessions alive every 10 seconds (Jellyfin considers sessions stale after ~15 seconds of inactivity) _keepAliveTimer = new Timer(KeepSessionsAlive, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); - _logger.LogWarning("πŸ”§ SESSION: JellyfinSessionManager initialized with 10-second keep-alive and WebSocket support"); + _logger.LogDebug("πŸ”§ SESSION: JellyfinSessionManager initialized with 10-second keep-alive and WebSocket support"); } /// @@ -51,17 +51,17 @@ public class JellyfinSessionManager : IDisposable if (_sessions.TryGetValue(deviceId, out var existingSession)) { existingSession.LastActivity = DateTime.UtcNow; - _logger.LogWarning("βœ“ SESSION: Session already exists for device {DeviceId}", deviceId); + _logger.LogDebug("βœ“ SESSION: Session already exists for device {DeviceId}", deviceId); // Refresh capabilities to keep session alive await PostCapabilitiesAsync(headers); return true; } - _logger.LogWarning("πŸ”§ SESSION: Creating new session for device: {DeviceId} ({Client} on {Device})", deviceId, client, device); + _logger.LogInformation("πŸ”§ SESSION: Creating new session for device: {DeviceId} ({Client} on {Device})", deviceId, client, device); // Log the headers we received for debugging - _logger.LogWarning("πŸ” SESSION: Headers received for session creation: {Headers}", + _logger.LogDebug("πŸ” SESSION: Headers received for session creation: {Headers}", string.Join(", ", headers.Select(h => $"{h.Key}={h.Value.ToString().Substring(0, Math.Min(30, h.Value.ToString().Length))}..."))); try @@ -69,7 +69,7 @@ public class JellyfinSessionManager : IDisposable // Post session capabilities to Jellyfin - this creates the session await PostCapabilitiesAsync(headers); - _logger.LogWarning("βœ“ SESSION: Session created for {DeviceId}", deviceId); + _logger.LogInformation("βœ“ SESSION: Session created for {DeviceId}", deviceId); // Track this session _sessions[deviceId] = new SessionInfo @@ -135,11 +135,11 @@ public class JellyfinSessionManager : IDisposable if (_sessions.TryGetValue(deviceId, out var session)) { session.LastActivity = DateTime.UtcNow; - _logger.LogWarning("πŸ”„ SESSION: Updated activity for {DeviceId}", deviceId); + _logger.LogDebug("πŸ”„ SESSION: Updated activity for {DeviceId}", deviceId); } else { - _logger.LogWarning("⚠️ SESSION: Cannot update activity - device {DeviceId} not found", deviceId); + _logger.LogDebug("⚠️ SESSION: Cannot update activity - device {DeviceId} not found", deviceId); } } @@ -150,7 +150,7 @@ public class JellyfinSessionManager : IDisposable { if (_sessions.TryRemove(deviceId, out var session)) { - _logger.LogWarning("πŸ—‘οΈ SESSION: Removing session for device {DeviceId}", deviceId); + _logger.LogInformation("πŸ—‘οΈ SESSION: Removing session for device {DeviceId}", deviceId); // Close WebSocket if it exists if (session.WebSocket != null && session.WebSocket.State == WebSocketState.Open) @@ -158,7 +158,7 @@ public class JellyfinSessionManager : IDisposable try { await session.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Session ended", CancellationToken.None); - _logger.LogWarning("πŸ”Œ WEBSOCKET: Closed WebSocket for device {DeviceId}", deviceId); + _logger.LogDebug("πŸ”Œ WEBSOCKET: Closed WebSocket for device {DeviceId}", deviceId); } catch (Exception ex) { @@ -178,7 +178,7 @@ public class JellyfinSessionManager : IDisposable } catch (Exception ex) { - _logger.LogWarning(ex, "⚠️ SESSION: Error removing session for {DeviceId}", deviceId); + _logger.LogWarning("⚠️ SESSION: Error removing session for {DeviceId}: {Message}", deviceId, ex.Message); } } } @@ -191,7 +191,7 @@ public class JellyfinSessionManager : IDisposable { if (!_sessions.TryGetValue(deviceId, out var session)) { - _logger.LogWarning("⚠️ WEBSOCKET: Cannot create WebSocket - session {DeviceId} not found", deviceId); + _logger.LogDebug("⚠️ WEBSOCKET: Cannot create WebSocket - session {DeviceId} not found", deviceId); return; } @@ -213,7 +213,7 @@ public class JellyfinSessionManager : IDisposable session.WebSocket = webSocket; // Log available headers for debugging - _logger.LogWarning("πŸ” WEBSOCKET: Available headers for {DeviceId}: {Headers}", + _logger.LogDebug("πŸ” WEBSOCKET: Available headers for {DeviceId}: {Headers}", deviceId, string.Join(", ", headers.Keys)); // Forward authentication headers from the CLIENT - this is critical for session to appear under the right user @@ -221,7 +221,7 @@ public class JellyfinSessionManager : IDisposable if (headers.TryGetValue("X-Emby-Authorization", out var embyAuth)) { webSocket.Options.SetRequestHeader("X-Emby-Authorization", embyAuth.ToString()); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Using X-Emby-Authorization for {DeviceId}: {Auth}", + _logger.LogDebug("πŸ”‘ WEBSOCKET: Using X-Emby-Authorization for {DeviceId}: {Auth}", deviceId, embyAuth.ToString().Length > 50 ? embyAuth.ToString()[..50] + "..." : embyAuth.ToString()); authFound = true; } @@ -231,14 +231,14 @@ public class JellyfinSessionManager : IDisposable if (authValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase)) { webSocket.Options.SetRequestHeader("X-Emby-Authorization", authValue); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Converted Authorization to X-Emby-Authorization for {DeviceId}: {Auth}", + _logger.LogDebug("πŸ”‘ WEBSOCKET: Converted Authorization to X-Emby-Authorization for {DeviceId}: {Auth}", deviceId, authValue.Length > 50 ? authValue[..50] + "..." : authValue); authFound = true; } else { webSocket.Options.SetRequestHeader("Authorization", authValue); - _logger.LogWarning("πŸ”‘ WEBSOCKET: Using Authorization for {DeviceId}: {Auth}", + _logger.LogDebug("πŸ”‘ WEBSOCKET: Using Authorization for {DeviceId}: {Auth}", deviceId, authValue.Length > 50 ? authValue[..50] + "..." : authValue); authFound = true; } @@ -258,14 +258,14 @@ public class JellyfinSessionManager : IDisposable } } - _logger.LogWarning("πŸ”— WEBSOCKET: Connecting to Jellyfin for device {DeviceId}: {Url}", deviceId, jellyfinWsUrl); + _logger.LogDebug("πŸ”— WEBSOCKET: Connecting to Jellyfin for device {DeviceId}: {Url}", deviceId, jellyfinWsUrl); // Set user agent webSocket.Options.SetRequestHeader("User-Agent", $"Allstarr-Proxy/{session.Client}"); // Connect to Jellyfin await webSocket.ConnectAsync(new Uri(jellyfinWsUrl), CancellationToken.None); - _logger.LogWarning("βœ“ WEBSOCKET: Connected to Jellyfin for device {DeviceId}", deviceId); + _logger.LogInformation("βœ“ WEBSOCKET: Connected to Jellyfin for device {DeviceId}", deviceId); // CRITICAL: Send ForceKeepAlive message to initialize session in Jellyfin // This tells Jellyfin to create/show the session in the dashboard @@ -273,13 +273,13 @@ public class JellyfinSessionManager : IDisposable var forceKeepAliveMessage = "{\"MessageType\":\"ForceKeepAlive\",\"Data\":100}"; var messageBytes = Encoding.UTF8.GetBytes(forceKeepAliveMessage); await webSocket.SendAsync(new ArraySegment(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None); - _logger.LogWarning("πŸ“€ WEBSOCKET: Sent ForceKeepAlive to initialize session for {DeviceId}", deviceId); + _logger.LogDebug("πŸ“€ WEBSOCKET: Sent ForceKeepAlive to initialize session for {DeviceId}", deviceId); // Also send SessionsStart to subscribe to session updates var sessionsStartMessage = "{\"MessageType\":\"SessionsStart\",\"Data\":\"0,1500\"}"; messageBytes = Encoding.UTF8.GetBytes(sessionsStartMessage); await webSocket.SendAsync(new ArraySegment(messageBytes), WebSocketMessageType.Text, true, CancellationToken.None); - _logger.LogWarning("πŸ“€ WEBSOCKET: Sent SessionsStart for {DeviceId}", deviceId); + _logger.LogDebug("πŸ“€ WEBSOCKET: Sent SessionsStart for {DeviceId}", deviceId); // Keep the WebSocket alive by reading messages and sending periodic keep-alive var buffer = new byte[1024 * 4]; @@ -300,7 +300,7 @@ public class JellyfinSessionManager : IDisposable if (result.MessageType == WebSocketMessageType.Close) { - _logger.LogWarning("πŸ”Œ WEBSOCKET: Jellyfin closed WebSocket for device {DeviceId}", deviceId); + _logger.LogDebug("πŸ”Œ WEBSOCKET: Jellyfin closed WebSocket for device {DeviceId}", deviceId); break; } @@ -338,7 +338,7 @@ public class JellyfinSessionManager : IDisposable var keepAliveMsg = "{\"MessageType\":\"KeepAlive\"}"; var keepAliveBytes = Encoding.UTF8.GetBytes(keepAliveMsg); await webSocket.SendAsync(new ArraySegment(keepAliveBytes), WebSocketMessageType.Text, true, CancellationToken.None); - _logger.LogWarning("πŸ’“ WEBSOCKET: Sent KeepAlive for {DeviceId}", deviceId); + _logger.LogDebug("πŸ’“ WEBSOCKET: Sent KeepAlive for {DeviceId}", deviceId); lastKeepAlive = DateTime.UtcNow; } } @@ -366,7 +366,7 @@ public class JellyfinSessionManager : IDisposable catch { } } webSocket.Dispose(); - _logger.LogWarning("🧹 WEBSOCKET: Cleaned up WebSocket for device {DeviceId}", deviceId); + _logger.LogDebug("🧹 WEBSOCKET: Cleaned up WebSocket for device {DeviceId}", deviceId); } // Clear WebSocket reference from session