添加静态日志

This commit is contained in:
xuejiaming 2022-06-10 20:36:48 +08:00
parent 980918efd3
commit 13a05a04c1
4 changed files with 62 additions and 14 deletions

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace ShardingCore.Logger
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/5/18 10:02:25
/// Email: 326308290@qq.com
public sealed class InternalLoggerFactory
{
static ILoggerFactory _defaultFactory;
static ILoggerFactory NewDefaultFactory()
{
var f = new NullLoggerFactory();
return f;
}
/// <summary>
/// Gets or sets the default factory.
/// </summary>
public static ILoggerFactory DefaultFactory
{
get
{
ILoggerFactory factory = Volatile.Read(ref _defaultFactory);
if (factory == null)
{
factory = NewDefaultFactory();
ILoggerFactory current = Interlocked.CompareExchange(ref _defaultFactory, factory, null);
if (current != null)
{
return current;
}
}
return factory;
}
set => Volatile.Write(ref _defaultFactory, value);
}
public static ILogger<T> CreateLogger<T>() => DefaultFactory.CreateLogger<T>();
public static ILogger CreateLogger(string categoryName) => DefaultFactory.CreateLogger(categoryName);
}
}

View File

@ -10,13 +10,15 @@ using System;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using ShardingCore.Extensions.InternalExtensions; using ShardingCore.Extensions.InternalExtensions;
using ShardingCore.Logger;
using ShardingCore.Sharding.Parsers.Abstractions; using ShardingCore.Sharding.Parsers.Abstractions;
namespace ShardingCore.Sharding.ShardingExecutors namespace ShardingCore.Sharding.ShardingExecutors
{ {
public class QueryCompilerContextFactory : IQueryCompilerContextFactory public class QueryCompilerContextFactory : IQueryCompilerContextFactory
{ {
private readonly ILogger<QueryCompilerContextFactory> _logger; private static readonly ILogger<QueryCompilerContextFactory> _logger =
InternalLoggerFactory.CreateLogger<QueryCompilerContextFactory>();
private static readonly IQueryableCombine _enumerableQueryableCombine; private static readonly IQueryableCombine _enumerableQueryableCombine;
private static readonly IQueryableCombine _allQueryableCombine; private static readonly IQueryableCombine _allQueryableCombine;
private static readonly IQueryableCombine _constantQueryableCombine; private static readonly IQueryableCombine _constantQueryableCombine;
@ -32,10 +34,6 @@ namespace ShardingCore.Sharding.ShardingExecutors
_whereQueryableCombine = new WhereQueryableCombine(); _whereQueryableCombine = new WhereQueryableCombine();
} }
public QueryCompilerContextFactory(ILogger<QueryCompilerContextFactory> logger)
{
_logger = logger;
}
public IQueryCompilerContext Create(IPrepareParseResult prepareParseResult) public IQueryCompilerContext Create(IPrepareParseResult prepareParseResult)
{ {

View File

@ -10,6 +10,7 @@ using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using System; using System;
using System.Threading; using System.Threading;
using ShardingCore.Logger;
namespace ShardingCore.TableCreator namespace ShardingCore.TableCreator
{ {
@ -21,14 +22,14 @@ namespace ShardingCore.TableCreator
*/ */
public class ShardingTableCreator<TShardingDbContext> : IShardingTableCreator<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext public class ShardingTableCreator<TShardingDbContext> : IShardingTableCreator<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{ {
private readonly ILogger<ShardingTableCreator<TShardingDbContext>> _logger; private static readonly ILogger<ShardingTableCreator<TShardingDbContext>> _logger =
InternalLoggerFactory.CreateLogger<ShardingTableCreator<TShardingDbContext>>();
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly IShardingEntityConfigOptions<TShardingDbContext> _entityConfigOptions; private readonly IShardingEntityConfigOptions<TShardingDbContext> _entityConfigOptions;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
public ShardingTableCreator(ILogger<ShardingTableCreator<TShardingDbContext>> logger, IServiceProvider serviceProvider, IShardingEntityConfigOptions<TShardingDbContext> entityConfigOptions, IRouteTailFactory routeTailFactory) public ShardingTableCreator(IServiceProvider serviceProvider, IShardingEntityConfigOptions<TShardingDbContext> entityConfigOptions, IRouteTailFactory routeTailFactory)
{ {
_logger = logger;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_entityConfigOptions = entityConfigOptions; _entityConfigOptions = entityConfigOptions;
_routeTailFactory = routeTailFactory; _routeTailFactory = routeTailFactory;

View File

@ -15,6 +15,7 @@ using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Jobs.Abstaractions; using ShardingCore.Jobs.Abstaractions;
using ShardingCore.Logger;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
namespace ShardingCore.VirtualRoutes.Abstractions namespace ShardingCore.VirtualRoutes.Abstractions
@ -22,13 +23,9 @@ namespace ShardingCore.VirtualRoutes.Abstractions
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public abstract class AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey> : AbstractShardingOperatorVirtualTableRoute<TEntity, TKey>, IJob where TEntity : class public abstract class AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey> : AbstractShardingOperatorVirtualTableRoute<TEntity, TKey>, IJob where TEntity : class
{ {
private readonly ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>> _logger; private static readonly ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>> _logger =
InternalLoggerFactory.CreateLogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>>();
protected AbstractShardingAutoCreateOperatorVirtualTableRoute()
{
_logger = ShardingContainer
.GetService<ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>>>();
}
/// <summary> /// <summary>
/// 不可以设置一样 /// 不可以设置一样
/// </summary> /// </summary>