Successfully added
C#
by Patrik
TryCombineUrl Logging Helper
TryCombineUrl
follows the .NET convention for “Try” methods by returning a bool
indicating success and providing the result via an out
parameter. This approach is ideal when logging failures explicitly or when avoiding exceptions is critical. It can be used in structured logging or conditional logging scenarios where fallback logic may be required.
public static class LoggingHelpers
{
public static bool TryCombineUrl(Uri? baseAddress, string path, out string? fullUrl)
{
try
{
if (string.IsNullOrWhiteSpace(path))
{
fullUrl = baseAddress?.ToString() ?? string.Empty;
}
else if (baseAddress == null)
{
fullUrl = path;
}
else
{
fullUrl = new Uri(baseAddress, path).ToString();
}
return true;
}
catch
{
fullUrl = null;
return false;
}
}
}
dotnet
trymethod
url
structuredlogging
cleanarchitecture
Referenced in:
Comments