initial commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Diagnostics;
|
||||
using LogViewer.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MsLogger.Core;
|
||||
|
||||
public class DataStoreLogger: ILogger
|
||||
{
|
||||
// ref: https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider
|
||||
|
||||
#region Constructor
|
||||
|
||||
public DataStoreLogger(string name, Func<DataStoreLoggerConfiguration> getCurrentConfig, ILogDataStore dataStore)
|
||||
{
|
||||
(_name, _getCurrentConfig) = (name, getCurrentConfig);
|
||||
_dataStore = dataStore;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private readonly ILogDataStore _dataStore;
|
||||
private readonly string _name;
|
||||
private readonly Func<DataStoreLoggerConfiguration> _getCurrentConfig;
|
||||
|
||||
#endregion
|
||||
|
||||
#region methods
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state) where TState : notnull => default!;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
// check if we are logging for passed log level
|
||||
if (!IsEnabled(logLevel))
|
||||
return;
|
||||
|
||||
DataStoreLoggerConfiguration config = _getCurrentConfig();
|
||||
|
||||
_dataStore.AddEntry(new()
|
||||
{
|
||||
Timestamp = DateTime.UtcNow,
|
||||
LogLevel = logLevel,
|
||||
// do we override the default EventId if it exists?
|
||||
EventId = eventId.Id == 0 && config.EventId != 0 ? config.EventId : eventId,
|
||||
State = state,
|
||||
Exception = exception?.Message ?? (logLevel == LogLevel.Error ? state?.ToString() ?? "" : ""),
|
||||
Color = config.Colors[logLevel],
|
||||
});
|
||||
|
||||
Debug.WriteLine($"--- [{logLevel.ToString()[..3]}] {_name} - {formatter(state, exception!)}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Concurrent;
|
||||
using LogViewer.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace MsLogger.Core;
|
||||
|
||||
public class DataStoreLoggerProvider: ILoggerProvider
|
||||
{
|
||||
|
||||
#region Constructor
|
||||
|
||||
public DataStoreLoggerProvider(IOptionsMonitor<DataStoreLoggerConfiguration> config, ILogDataStore dataStore)
|
||||
{
|
||||
_dataStore = dataStore;
|
||||
_currentConfig = config.CurrentValue;
|
||||
_onChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region fields
|
||||
|
||||
private DataStoreLoggerConfiguration _currentConfig;
|
||||
|
||||
private readonly IDisposable? _onChangeToken;
|
||||
protected readonly ILogDataStore _dataStore;
|
||||
|
||||
protected readonly ConcurrentDictionary<string, DataStoreLogger> _loggers = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
=> _loggers.GetOrAdd(categoryName, name => new DataStoreLogger(name, GetCurrentConfig, _dataStore));
|
||||
|
||||
protected DataStoreLoggerConfiguration GetCurrentConfig()
|
||||
=> _currentConfig;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_loggers.Clear();
|
||||
_onChangeToken?.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using LogViewer.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MsLogger.Core;
|
||||
|
||||
public static class ServicesExtension
|
||||
{
|
||||
public static ILoggingBuilder AddDefaultDataStoreLogger(this ILoggingBuilder builder)
|
||||
{
|
||||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, DataStoreLoggerProvider>());
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static ILoggingBuilder AddDefaultDataStoreLogger(this ILoggingBuilder builder, Action<DataStoreLoggerConfiguration> configure)
|
||||
{
|
||||
builder.AddDefaultDataStoreLogger();
|
||||
builder.Services.Configure(configure);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LogViewer.Core\LogViewer.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user