in src/log4net/Core/DefaultRepositorySelector.cs [178:290]
public ILoggerRepository CreateRepository(Assembly assembly, Type repositoryType)
=> CreateRepository(assembly, repositoryType, DefaultRepositoryName, true);
/// <summary>
/// Creates a new repository for the assembly specified.
/// </summary>
/// <param name="repositoryAssembly">the assembly to use to create the repository to associate with the <see cref="ILoggerRepository"/>.</param>
/// <param name="repositoryType">The type of repository to create, must implement <see cref="ILoggerRepository"/>.</param>
/// <param name="repositoryName">The name to assign to the created repository</param>
/// <param name="readAssemblyAttributes">Set to <c>true</c> to read and apply the assembly attributes</param>
/// <returns>The repository created.</returns>
/// <remarks>
/// <para>
/// The <see cref="ILoggerRepository"/> created will be associated with the repository
/// specified such that a call to <see cref="GetRepository(Assembly)"/> with the
/// same assembly specified will return the same repository instance.
/// </para>
/// <para>
/// The type of the <see cref="ILoggerRepository"/> created and
/// the repository to create can be overridden by specifying the
/// <see cref="RepositoryAttribute"/> attribute on the
/// <paramref name="repositoryAssembly"/>. The default values are to use the
/// <paramref name="repositoryType"/> implementation of the
/// <see cref="ILoggerRepository"/> interface and to use the
/// <see cref="AssemblyName.Name"/> as the name of the repository.
/// </para>
/// <para>
/// The <see cref="ILoggerRepository"/> created will be automatically
/// configured using any <see cref="ConfiguratorAttribute"/>
/// attributes defined on the <paramref name="repositoryAssembly"/>.
/// </para>
/// <para>
/// If a repository for the <paramref name="repositoryAssembly"/> already exists
/// that repository will be returned. An error will not be raised and that
/// repository may be of a different type to that specified in <paramref name="repositoryType"/>.
/// Also the <see cref="RepositoryAttribute"/> attribute on the
/// assembly may be used to override the repository type specified in
/// <paramref name="repositoryType"/>.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="repositoryAssembly"/> is <see langword="null" />.</exception>
public ILoggerRepository CreateRepository(Assembly repositoryAssembly, Type? repositoryType, string repositoryName, bool readAssemblyAttributes)
{
repositoryAssembly.EnsureNotNull();
repositoryType ??= _defaultRepositoryType;
lock (_syncRoot)
{
if (!_assembly2Repository.TryGetValue(repositoryAssembly, out ILoggerRepository? rep))
{
// Not found, therefore create
LogLog.Debug(_declaringType, $"Creating repository for assembly [{repositoryAssembly}]");
// Must specify defaults
string actualRepositoryName = repositoryName;
Type actualRepositoryType = repositoryType;
if (readAssemblyAttributes)
{
// Get the repository and type from the assembly attributes
GetInfoForAssembly(repositoryAssembly, ref actualRepositoryName, ref actualRepositoryType);
}
LogLog.Debug(_declaringType, $"Assembly [{repositoryAssembly}] using repository [{actualRepositoryName}] and repository type [{actualRepositoryType}]");
// Lookup the repository in the map (as this may already be defined)
if (!_name2Repository.TryGetValue(actualRepositoryName, out rep))
{
// Create the repository
rep = CreateRepository(actualRepositoryName, actualRepositoryType);
if (readAssemblyAttributes)
{
try
{
// Look for aliasing attributes
LoadAliases(repositoryAssembly, rep);
// Look for plugins defined on the assembly
LoadPlugins(repositoryAssembly, rep);
// Configure the repository using the assembly attributes
ConfigureRepository(repositoryAssembly, rep);
}
catch (Exception e) when (!e.IsFatal())
{
LogLog.Error(_declaringType, $"Failed to configure repository [{actualRepositoryName}] from assembly attributes.", e);
}
}
}
else
{
LogLog.Debug(_declaringType, $"repository [{actualRepositoryName}] already exists, using repository type [{rep.GetType().FullName}]");
if (readAssemblyAttributes)
{
try
{
// Look for plugins defined on the assembly
LoadPlugins(repositoryAssembly, rep);
}
catch (Exception e) when (!e.IsFatal())
{
LogLog.Error(_declaringType, $"Failed to configure repository [{actualRepositoryName}] from assembly attributes.", e);
}
}
}
_assembly2Repository[repositoryAssembly] = rep;
}
return rep;
}
}