Add SparkLoggerAdapter for Msft ILogger
This commit is contained in:
1 parent
cac881ea1b
commit
7fb411adce
2 files changed
+74
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using System;
|
||||
|
||||
using MsLogLevel = Microsoft.Extensions.Logging.LogLevel;
|
||||
using SparkLogger = bts.Logger;
|
||||
using SparkLogCategory = bts.LogSubsystem;
|
||||
using SparkLogLevel = bts.LogLevel;
|
||||
using bts;
|
||||
|
||||
class SparkLoggerAdapter : ILogger {
|
||||
readonly string categoryName;
|
||||
readonly MsLogLevel level;
|
||||
|
||||
public SparkLoggerAdapter(string categoryName, MsLogLevel level = MsLogLevel.Debug) {
|
||||
this.categoryName = categoryName;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull =>
|
||||
null;
|
||||
|
||||
public bool IsEnabled(MsLogLevel logLevel) =>
|
||||
logLevel >= level;
|
||||
|
||||
public void Log<TState>(MsLogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) =>
|
||||
getSparkLogger(categoryName).log_at(toSparkLogLevel(logLevel), formatter(state, exception));
|
||||
|
||||
static SparkLogLevel toSparkLogLevel(MsLogLevel logLevel) => logLevel switch {
|
||||
MsLogLevel.Trace or MsLogLevel.Debug => SparkLogLevel.Diag,
|
||||
MsLogLevel.Information => SparkLogLevel.Info,
|
||||
MsLogLevel.Warning => SparkLogLevel.Warn,
|
||||
MsLogLevel.Error => SparkLogLevel.Error,
|
||||
MsLogLevel.Critical => SparkLogLevel.Fatal,
|
||||
MsLogLevel.None => SparkLogLevel.NoLog,
|
||||
_ => throw new ArgumentException($"Invalid logLevel {logLevel}", nameof(logLevel)),
|
||||
};
|
||||
|
||||
static SparkLogCategory toSparkCategory(string categoryName) =>
|
||||
Enum.TryParse<SparkLogCategory>(categoryName, out var subsys) ? subsys : SparkLogCategory.gen;
|
||||
|
||||
static SparkLogger getSparkLogger(string categoryName) =>
|
||||
toSparkCategory(categoryName) switch {
|
||||
SparkLogCategory.Sdk => bts.Log.Sdk,
|
||||
SparkLogCategory.Http => bts.Log.Http,
|
||||
SparkLogCategory.gen => bts.Log.Gen,
|
||||
SparkLogCategory.Config => bts.Log.Config,
|
||||
SparkLogCategory.QBus => bts.Log.QBus,
|
||||
SparkLogCategory.md => bts.Log.Md,
|
||||
SparkLogCategory.Api => bts.Log.Api,
|
||||
SparkLogCategory.Persist => bts.Log.Persist,
|
||||
SparkLogCategory.User => bts.Log.User,
|
||||
SparkLogCategory.OrderMan => bts.Log.OrderMan,
|
||||
SparkLogCategory.Assert => bts.Log.Assert,
|
||||
SparkLogCategory.Ui => bts.Log.Ui,
|
||||
SparkLogCategory.Testing => bts.Log.Testing,
|
||||
SparkLogCategory.DataFlow => bts.Log.DataFlow,
|
||||
_ => throw new ArgumentException($"Invalid log category '{categoryName}'", categoryName),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public sealed class SparkLoggerProvider : ILoggerProvider {
|
||||
public ILogger CreateLogger(string categoryName) =>
|
||||
new SparkLoggerAdapter(categoryName);
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
Reference in new issue
Block a user