Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public record struct CreateLoggerOptions
public string Name { get; init; }

/// <summary>
/// The context type of the logger.
/// The context name of the logger. This takes priority over <see cref="ContextType"/>.
/// </summary>
public Type ContextType { get; init; }
public string? ContextName { get; init; }

/// <summary>
/// The context type of the logger. This will be ignored if <see cref="ContextName"/> is set.
/// </summary>
public Type? ContextType { get; init; }
}
23 changes: 18 additions & 5 deletions Intersect (Core)/Plugins/Helpers/LoggingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static Logger CreateLogger(IManifestHelper manifest, CreateLoggerOptions

public ILogger Plugin { get; }

internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest)
internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest, PluginReference pluginReference)
{
mManifest = manifest;

Expand All @@ -60,15 +60,28 @@ internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest)
new CreateLoggerOptions
{
Console = Debugger.IsAttached ? LogLevel.Debug : LogLevel.None, File = LogLevel.Information,
ContextName = manifest.Name,
ContextType = pluginReference.EntryType,
}
);
}

/// <inheritdoc />
public ILogger CreateLogger(CreateLoggerOptions createLoggerOptions) =>
new SerilogLoggerFactory(CreateLogger(mManifest, createLoggerOptions)).CreateLogger(
createLoggerOptions.ContextType
);
public ILogger CreateLogger(CreateLoggerOptions createLoggerOptions)
{
var factory = new SerilogLoggerFactory(CreateLogger(mManifest, createLoggerOptions));
if (createLoggerOptions.ContextName is { } contextName && !string.IsNullOrWhiteSpace(contextName))
{
return factory.CreateLogger(contextName);
}

if (createLoggerOptions.ContextType is { } contextType)
{
return factory.CreateLogger(contextType);
}

return factory.CreateLogger("Plugin");
}

/// <inheritdoc />
public ILogger<TContext> CreateLogger<TContext>(CreateLoggerOptions createLoggerOptions) =>
Expand Down
2 changes: 1 addition & 1 deletion Intersect (Core)/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static Plugin Create(
IApplicationContext applicationContext,
IManifestHelper manifest,
PluginReference reference
) => new Plugin(manifest, new LoggingHelper(applicationContext.Logger, manifest), reference);
) => new Plugin(manifest, new LoggingHelper(applicationContext.Logger, manifest, reference), reference);

// ReSharper disable once NotNullMemberIsNotInitialized
// Plugin instance is created at the Discovery phase and Configuration is loaded afterwards
Expand Down
Loading