Skip to content

Commit ea7b4f8

Browse files
committed
fix: add missing context information for Plugin logging
1 parent 0b4e878 commit ea7b4f8

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Framework/Intersect.Framework.Core/Plugins/Interfaces/CreateLoggerOptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public record struct CreateLoggerOptions
2424
public string Name { get; init; }
2525

2626
/// <summary>
27-
/// The context type of the logger.
27+
/// The context name of the logger. This takes priority over <see cref="ContextType"/>.
2828
/// </summary>
29-
public Type ContextType { get; init; }
29+
public string? ContextName { get; init; }
30+
31+
/// <summary>
32+
/// The context type of the logger. This will be ignored if <see cref="ContextName"/> is set.
33+
/// </summary>
34+
public Type? ContextType { get; init; }
3035
}

Intersect (Core)/Plugins/Helpers/LoggingHelper.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static Logger CreateLogger(IManifestHelper manifest, CreateLoggerOptions
5151

5252
public ILogger Plugin { get; }
5353

54-
internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest)
54+
internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest, PluginReference pluginReference)
5555
{
5656
mManifest = manifest;
5757

@@ -60,15 +60,28 @@ internal LoggingHelper(ILogger applicationLogger, IManifestHelper manifest)
6060
new CreateLoggerOptions
6161
{
6262
Console = Debugger.IsAttached ? LogLevel.Debug : LogLevel.None, File = LogLevel.Information,
63+
ContextName = manifest.Name,
64+
ContextType = pluginReference.EntryType,
6365
}
6466
);
6567
}
6668

6769
/// <inheritdoc />
68-
public ILogger CreateLogger(CreateLoggerOptions createLoggerOptions) =>
69-
new SerilogLoggerFactory(CreateLogger(mManifest, createLoggerOptions)).CreateLogger(
70-
createLoggerOptions.ContextType
71-
);
70+
public ILogger CreateLogger(CreateLoggerOptions createLoggerOptions)
71+
{
72+
var factory = new SerilogLoggerFactory(CreateLogger(mManifest, createLoggerOptions));
73+
if (createLoggerOptions.ContextName is { } contextName && !string.IsNullOrWhiteSpace(contextName))
74+
{
75+
return factory.CreateLogger(contextName);
76+
}
77+
78+
if (createLoggerOptions.ContextType is { } contextType)
79+
{
80+
return factory.CreateLogger(contextType);
81+
}
82+
83+
return factory.CreateLogger("Plugin");
84+
}
7285

7386
/// <inheritdoc />
7487
public ILogger<TContext> CreateLogger<TContext>(CreateLoggerOptions createLoggerOptions) =>

Intersect (Core)/Plugins/Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static Plugin Create(
2020
IApplicationContext applicationContext,
2121
IManifestHelper manifest,
2222
PluginReference reference
23-
) => new Plugin(manifest, new LoggingHelper(applicationContext.Logger, manifest), reference);
23+
) => new Plugin(manifest, new LoggingHelper(applicationContext.Logger, manifest, reference), reference);
2424

2525
// ReSharper disable once NotNullMemberIsNotInitialized
2626
// Plugin instance is created at the Discovery phase and Configuration is loaded afterwards

0 commit comments

Comments
 (0)