in src/log4net/Config/XmlConfiguratorAttribute.cs [301:403]
private void ConfigureFromUri(ILoggerRepository targetRepository)
{
// Work out the full path to the config file
Uri? fullPath2ConfigFile = null;
// Select the config file
if (string.IsNullOrEmpty(ConfigFile))
{
if (string.IsNullOrEmpty(ConfigFileExtension))
{
string? systemConfigFilePath = null;
try
{
systemConfigFilePath = SystemInfo.ConfigurationFileLocation;
}
catch (Exception e) when (!e.IsFatal())
{
LogLog.Error(_declaringType, "XmlConfiguratorAttribute: Exception getting ConfigurationFileLocation. Must be able to resolve ConfigurationFileLocation when ConfigFile and ConfigFileExtension properties are not set.", e);
}
if (systemConfigFilePath is not null)
{
// Use the default .config file for the AppDomain
fullPath2ConfigFile = new Uri(systemConfigFilePath);
}
}
else
{
// Force the extension to start with a '.'
if (ConfigFileExtension![0] != '.')
{
ConfigFileExtension = '.' + ConfigFileExtension;
}
string? systemConfigFilePath = null;
try
{
systemConfigFilePath = SystemInfo.ConfigurationFileLocation;
}
catch (Exception e) when (!e.IsFatal())
{
LogLog.Error(_declaringType, "XmlConfiguratorAttribute: Exception getting ConfigurationFileLocation. Must be able to resolve ConfigurationFileLocation when the ConfigFile property are not set.", e);
}
if (systemConfigFilePath is not null)
{
var builder = new UriBuilder(new Uri(systemConfigFilePath));
// Remove the current extension from the systemConfigFileUri path
string path = builder.Path;
int startOfExtension = path.LastIndexOf('.');
if (startOfExtension >= 0)
{
path = path.Substring(0, startOfExtension);
}
path += ConfigFileExtension;
builder.Path = path;
fullPath2ConfigFile = builder.Uri;
}
}
}
else
{
string? applicationBaseDirectory = null;
try
{
applicationBaseDirectory = SystemInfo.ApplicationBaseDirectory;
}
catch (Exception e) when (!e.IsFatal())
{
LogLog.Warn(_declaringType, $"Exception getting ApplicationBaseDirectory. ConfigFile property path [{ConfigFile}] will be treated as an absolute URI.", e);
}
if (applicationBaseDirectory is not null)
{
// Just the base dir + the config file
fullPath2ConfigFile = new Uri(new Uri(applicationBaseDirectory), ConfigFile!);
}
else
{
fullPath2ConfigFile = new Uri(ConfigFile!);
}
}
if (fullPath2ConfigFile is not null)
{
if (fullPath2ConfigFile.IsFile)
{
// The ConfigFile could be an absolute local path, therefore we have to be
// prepared to switch back to using FileInfos here
ConfigureFromFile(targetRepository, new FileInfo(fullPath2ConfigFile.LocalPath));
}
else
{
if (Watch)
{
LogLog.Warn(_declaringType, "XmlConfiguratorAttribute: Unable to watch config file loaded from a URI");
}
XmlConfigurator.Configure(targetRepository, fullPath2ConfigFile);
}
}
}