From 5f9b6380bbfed4e0782ced0c32e42bdd4deffc1b Mon Sep 17 00:00:00 2001 From: Paulo Morgado <470455+paulomorgado@users.noreply.github.com> Date: Thu, 4 Jun 2026 12:06:37 +0100 Subject: [PATCH] Refactor logging to use structured logging patterns Replaced string interpolation in logger calls with structured logging, removed redundant ToString() calls, and simplified string formatting throughout. Improved consistency and readability in log and exception messages. --- src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs | 6 ++---- src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs | 6 +++--- src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs | 15 ++++++--------- .../Interop/MacOs/AvFoundation.cs | 4 ++-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs b/src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs index 3a85b3919..d6e68f960 100644 --- a/src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs +++ b/src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs @@ -42,9 +42,7 @@ public unsafe FFmpegCameraSource(Camera camera) string inputFormat = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dshow" : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "v4l2" : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "avfoundation" - : throw new NotSupportedException($"Cannot find adequate input format" + - $" - OSArchitecture:[{RuntimeInformation.OSArchitecture}]" + - $" - OSDescription:[{RuntimeInformation.OSDescription}]"); + : throw new NotSupportedException($"Cannot find adequate input format - OSArchitecture:[{RuntimeInformation.OSArchitecture}] - OSDescription:[{RuntimeInformation.OSDescription}]"); var _aVInputFormat = ffmpeg.av_find_input_format(inputFormat); @@ -126,7 +124,7 @@ public bool RestrictCameraOptions(Func, bool> optFilt ); if (filtered is null) - logger.LogWarning($"No camera/input device options to be used."); + logger.LogWarning("No camera/input device options to be used."); return SetCameraDeviceOptions(filtered); } diff --git a/src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs b/src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs index 33d619155..168199353 100644 --- a/src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs +++ b/src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs @@ -28,7 +28,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20 { ["offset_x"] = rect.X.ToString(), ["offset_y"] = rect.Y.ToString(), - ["video_size"] = $"{rect.Width.ToString()}X{rect.Height.ToString()}", + ["video_size"] = $"{rect.Width}X{rect.Height}", ["framerate"] = frameRate.ToString() }; } @@ -37,7 +37,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20 inputFormat = "avfoundation"; options = new Dictionary() { - ["vf"] = $"crop={rect.Width.ToString()}:{rect.Height.ToString()}:{rect.X.ToString()}:{rect.Y.ToString()}", + ["vf"] = $"crop={rect.Width}:{rect.Height}:{rect.X}:{rect.Y}", ["framerate"] = frameRate.ToString() }; } @@ -47,7 +47,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20 //https://superuser.com/questions/1562228/how-to-specify-the-size-to-record-the-screen-with-ffmpeg options = new Dictionary() { - ["video_size"] = $"{rect.Width.ToString()}X{rect.Height.ToString()}", + ["video_size"] = $"{rect.Width}X{rect.Height}", ["grab_x"] = rect.X.ToString(), ["grab_y"] = rect.Y.ToString(), ["framerate"] = frameRate.ToString() diff --git a/src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs b/src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs index cc1c04eee..2b27a64e0 100644 --- a/src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs +++ b/src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs @@ -83,7 +83,7 @@ public static void Initialise(FfmpegLogLevelEnum? logLevel = null, String? libPa RegisterFFmpegBinaries(libPath); - logger.LogInformation($"FFmpeg version info: {ffmpeg.av_version_info()}"); + logger.LogInformation("FFmpeg version info: {VersionInfo}", ffmpeg.av_version_info()); if (logLevel.HasValue) { @@ -105,10 +105,7 @@ internal static void SetFFmpegBinariesPath(string path) catch (Exception e) { throw new DllNotFoundException( - "Check the dependencies of FFmpeg libraries and make sure they are " + - "searchable by the operating system's library loader." - + "\nOn linux you can use 'ldd' & 'strace'." - + "\nOn Windows you can use 'Dependencies'." + $"Check the dependencies of FFmpeg libraries and make sure they are searchable by the operating system's library loader.\nOn linux you can use 'ldd' & 'strace'.\nOn Windows you can use 'Dependencies'." , e); } } @@ -124,12 +121,12 @@ internal static void RegisterFFmpegBinaries(String? libPath = null) string ffmpegExecutable = "ffmpeg"; string? path = Environment.GetEnvironmentVariable("PATH")? .Split([';'], StringSplitOptions.RemoveEmptyEntries) - .Where(s => File.Exists(Path.Combine(s, ffmpegExecutable)) || File.Exists(Path.Combine(s, ffmpegExecutable + ".exe"))) + .Where(s => File.Exists(Path.Combine(s, ffmpegExecutable)) || File.Exists(Path.Combine(s, $"{ffmpegExecutable}.exe"))) .FirstOrDefault(); if (path != null) { - logger.LogInformation($"FFmpeg binaries found in system path at: {path}"); + logger.LogInformation("FFmpeg binaries found in system path at: {Path}", path); SetFFmpegBinariesPath(path); return; } @@ -142,7 +139,7 @@ internal static void RegisterFFmpegBinaries(String? libPath = null) var ffmpegBinaryPath = Path.Combine(current, probe); if (Directory.Exists(ffmpegBinaryPath)) { - logger.LogInformation($"FFmpeg binaries found in: {ffmpegBinaryPath}"); + logger.LogInformation("FFmpeg binaries found in: {Path}", ffmpegBinaryPath); SetFFmpegBinariesPath(ffmpegBinaryPath); return; } @@ -154,7 +151,7 @@ internal static void RegisterFFmpegBinaries(String? libPath = null) { if (Directory.Exists(libPath)) { - logger.LogInformation($"FFmpeg binaries path set to: {libPath}"); + logger.LogInformation("FFmpeg binaries path set to: {Path}", libPath); SetFFmpegBinariesPath(libPath); return; } diff --git a/src/SIPSorceryMedia.FFmpeg/Interop/MacOs/AvFoundation.cs b/src/SIPSorceryMedia.FFmpeg/Interop/MacOs/AvFoundation.cs index 8a9e67ec4..6c5d44ee7 100644 --- a/src/SIPSorceryMedia.FFmpeg/Interop/MacOs/AvFoundation.cs +++ b/src/SIPSorceryMedia.FFmpeg/Interop/MacOs/AvFoundation.cs @@ -85,7 +85,7 @@ private static unsafe String GetAvFoundationLogsAboutDevicesList() { index = ln.IndexOf("]"); string name = ln.Substring(index + 2); - string path = ln.Substring(1, index - 1) + ":"; + string path = $"{ln.Substring(1, index - 1)}:"; Monitor monitor = new Monitor { @@ -149,7 +149,7 @@ private static unsafe String GetAvFoundationLogsAboutDevicesList() { index = ln.IndexOf("]"); string name = ln.Substring(index+2); - string path = ln.Substring(1, index - 1) + ":"; + string path = $"{ln.Substring(1, index - 1)}:"; Camera camera = new Camera {