Successfully added
C#
by Patrik
CombineUrl Logging Helper
The CombineUrl
method offers a safe, reusable way to concatenate a base URI and path string for logging purposes. It is wrapped in a try-catch
block to avoid exceptions from malformed input, making it reliable for use in production logging. This method returns a string and can be placed inside a centralized LoggingHelpers
class for consistent use across the codebase.
public static class LoggingHelpers
{
public static string CombineUrl(Uri? baseAddress, string path)
{
try
{
if (string.IsNullOrWhiteSpace(path)) return baseAddress?.ToString() ?? string.Empty;
if (baseAddress == null) return path;
return new Uri(baseAddress, path).ToString();
}
catch (Exception ex)
{
return $"[InvalidUrl: {ex.Message}]";
}
}
}
csharp
url
logging
helper
exceptionhandling
Referenced in:
Comments