Skip to content

Commit

Permalink
Merge pull request #61 from bing-framework/dev_3.1
Browse files Browse the repository at this point in the history
Dev 3.1
  • Loading branch information
jianxuanbing authored Sep 28, 2022
2 parents 6cffb34 + 85c1fc5 commit ebac843
Show file tree
Hide file tree
Showing 23 changed files with 566 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,36 @@ public static void AddApiInterfaceService<TApiInterfaceService>(this IServiceCol
/// <param name="services">服务集合</param>
/// <param name="setupAction">配置操作</param>
public static void AddRequestResponseLog(this IServiceCollection services, Action<RequestResponseLoggerOptions> setupAction)
{
AddRequestResponseLog<DefaultRequestResponseLogger, DefaultRequestResponseLogCreator>(services, setupAction);
}

/// <summary>
/// 注册请求响应日志服务
/// </summary>
/// <typeparam name="TLogger">请求响应日志记录器</typeparam>
/// <param name="services">服务集合</param>
/// <param name="setupAction">配置操作</param>
public static void AddRequestResponseLog<TLogger>(this IServiceCollection services, Action<RequestResponseLoggerOptions> setupAction)
where TLogger : class, IRequestResponseLogger
{
AddRequestResponseLog<TLogger, DefaultRequestResponseLogCreator>(services, setupAction);
}

/// <summary>
/// 注册请求响应日志服务
/// </summary>
/// <typeparam name="TLogger">请求响应日志记录器</typeparam>
/// <typeparam name="TLogCreator">请求响应日志创建器</typeparam>
/// <param name="services">服务集合</param>
/// <param name="setupAction">配置操作</param>
public static void AddRequestResponseLog<TLogger, TLogCreator>(this IServiceCollection services, Action<RequestResponseLoggerOptions> setupAction)
where TLogger : class, IRequestResponseLogger
where TLogCreator : class, IRequestResponseLogCreator
{
services.Configure(setupAction);
services.AddSingleton<IRequestResponseLogger, DefaultRequestResponseLogger>();
services.AddScoped<IRequestResponseLogCreator, DefaultRequestResponseLogCreator>();
services.AddSingleton<IRequestResponseLogger, TLogger>();
services.AddScoped<IRequestResponseLogCreator, TLogCreator>();
}
}
}
24 changes: 24 additions & 0 deletions framework/src/Bing.Logging/Bing/Logging/BingLoggingBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.DependencyInjection;

namespace Bing.Logging
{
/// <summary>
/// 日志构建器
/// </summary>
public sealed class BingLoggingBuilder
{
/// <summary>
/// 初始化一个<see cref="BingLoggingBuilder"/>类型的实例
/// </summary>
/// <param name="services">服务集合</param>
public BingLoggingBuilder(IServiceCollection services)
{
Services = services;
}

/// <summary>
/// 服务集合
/// </summary>
public IServiceCollection Services { get; }
}
}
41 changes: 41 additions & 0 deletions framework/src/Bing.Logging/Bing/Logging/BingLoggingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;

namespace Bing.Logging
{
/// <summary>
/// Bing 日志选项配置
/// </summary>
public class BingLoggingOptions
{
/// <summary>
/// 初始化一个<see cref="BingLoggingOptions"/>类型的实例
/// </summary>
public BingLoggingOptions()
{
ClearProviders = false;
Extensions = new List<IBingLoggingOptionsExtension>();
}

/// <summary>
/// 日志选项扩展列表
/// </summary>
internal IList<IBingLoggingOptionsExtension> Extensions { get; }

/// <summary>
/// 是否清空日志提供程序
/// </summary>
public bool ClearProviders { get; set; }

/// <summary>
/// 注册扩展
/// </summary>
/// <param name="extension">日志选项配置扩展</param>
public void RegisterExtension(IBingLoggingOptionsExtension extension)
{
if (extension == null)
throw new ArgumentNullException(nameof(extension));
Extensions.Add(extension);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;

namespace Bing.Logging
{
/// <summary>
/// Bing 日志选项配置扩展
/// </summary>
public interface IBingLoggingOptionsExtension
{
/// <summary>
/// 注册子服务
/// </summary>
/// <param name="services">服务集合</param>
void AddServices(IServiceCollection services);
}
}
34 changes: 20 additions & 14 deletions framework/src/Bing.Logging/Bing/Logging/ILog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Bing.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Bing.Logging
Expand All @@ -8,45 +7,52 @@ namespace Bing.Logging
/// 日志操作
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
public interface ILog<out TCategoryName> : ITransientDependency
public interface ILog<out TCategoryName> : ILog
{
}

/// <summary>
/// 日志操作
/// </summary>
public interface ILog
{
/// <summary>
/// 设置日志事件标识
/// </summary>
/// <param name="eventId">日志事件标识</param>
ILog<TCategoryName> EventId(EventId eventId);
ILog EventId(EventId eventId);

/// <summary>
/// 设置异常
/// </summary>
/// <param name="exception">异常</param>
ILog<TCategoryName> Exception(Exception exception);
ILog Exception(Exception exception);

/// <summary>
/// 设置自定义扩展属性
/// </summary>
/// <param name="propertyName">属性名</param>
/// <param name="propertyValue">属性值</param>
ILog<TCategoryName> Property(string propertyName, string propertyValue);
ILog Property(string propertyName, string propertyValue);

/// <summary>
/// 设置标签
/// </summary>
/// <param name="tags">标签</param>
ILog<TCategoryName> Tags(params string[] tags);
ILog Tags(params string[] tags);

/// <summary>
/// 设置日志状态对象
/// </summary>
/// <param name="state">状态对象</param>
ILog<TCategoryName> State(object state);
ILog State(object state);

/// <summary>
/// 设置日志消息
/// </summary>
/// <param name="message">日志消息</param>
/// <param name="args">日志消息参数</param>
ILog<TCategoryName> Message(string message, params object[] args);
ILog Message(string message, params object[] args);

/// <summary>
/// 是否启用
Expand All @@ -65,31 +71,31 @@ public interface ILog<out TCategoryName> : ITransientDependency
/// <summary>
/// 写跟踪日志
/// </summary>
ILog<TCategoryName> LogTrace();
ILog LogTrace();

/// <summary>
/// 写调试日志
/// </summary>
ILog<TCategoryName> LogDebug();
ILog LogDebug();

/// <summary>
/// 写信息日志
/// </summary>
ILog<TCategoryName> LogInformation();
ILog LogInformation();

/// <summary>
/// 写警告日志
/// </summary>
ILog<TCategoryName> LogWarning();
ILog LogWarning();

/// <summary>
/// 写错误日志
/// </summary>
ILog<TCategoryName> LogError();
ILog LogError();

/// <summary>
/// 写致命日志
/// </summary>
ILog<TCategoryName> LogCritical();
ILog LogCritical();
}
}
15 changes: 5 additions & 10 deletions framework/src/Bing.Logging/Bing/Logging/ILogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ public static class ILogExtensions
/// <summary>
/// 添加消息
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
/// <param name="log">日志操作</param>
/// <param name="message">消息</param>
/// <param name="args">日志消息参数</param>
public static ILog<TCategoryName> Append<TCategoryName>(this ILog<TCategoryName> log, string message, params object[] args)
public static ILog Append(this ILog log, string message, params object[] args)
{
if (log is null)
throw new ArgumentNullException(nameof(log));
Expand All @@ -25,12 +24,11 @@ public static ILog<TCategoryName> Append<TCategoryName>(this ILog<TCategoryName>
/// <summary>
/// 添加消息,当条件为true时
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
/// <param name="log">日志操作</param>
/// <param name="message">消息</param>
/// <param name="condition">条件,值为true时,则添加消息</param>
/// <param name="args">日志消息参数</param>
public static ILog<TCategoryName> AppendIf<TCategoryName>(this ILog<TCategoryName> log, string message, bool condition, params object[] args)
public static ILog AppendIf(this ILog log, string message, bool condition, params object[] args)
{
if (log is null)
throw new ArgumentNullException(nameof(log));
Expand All @@ -42,11 +40,10 @@ public static ILog<TCategoryName> AppendIf<TCategoryName>(this ILog<TCategoryNam
/// <summary>
/// 添加消息并换行
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
/// <param name="log">日志操作</param>
/// <param name="message">消息</param>
/// <param name="args">日志消息参数</param>
public static ILog<TCategoryName> AppendLine<TCategoryName>(this ILog<TCategoryName> log, string message, params object[] args)
public static ILog AppendLine(this ILog log, string message, params object[] args)
{
if (log is null)
throw new ArgumentNullException(nameof(log));
Expand All @@ -58,13 +55,12 @@ public static ILog<TCategoryName> AppendLine<TCategoryName>(this ILog<TCategoryN
/// <summary>
/// 添加消息并换行,当条件为true时
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
/// <param name="log">日志操作</param>
/// <param name="message">消息</param>
/// <param name="condition">条件,值为true时,则添加消息</param>
/// <param name="args">日志消息参数</param>
/// <returns></returns>
public static ILog<TCategoryName> AppendLineIf<TCategoryName>(this ILog<TCategoryName> log, string message, bool condition, params object[] args)
public static ILog AppendLineIf(this ILog log, string message, bool condition, params object[] args)
{
if (log is null)
throw new ArgumentNullException(nameof(log));
Expand All @@ -79,9 +75,8 @@ public static ILog<TCategoryName> AppendLineIf<TCategoryName>(this ILog<TCategor
/// <summary>
/// 消息换行
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
/// <param name="log">日志操作</param>
public static ILog<TCategoryName> Line<TCategoryName>(this ILog<TCategoryName> log)
public static ILog Line(this ILog log)
{
if (log is null)
throw new ArgumentNullException(nameof(log));
Expand Down
22 changes: 22 additions & 0 deletions framework/src/Bing.Logging/Bing/Logging/ILogFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Bing.Logging
{
/// <summary>
/// 日志操作工厂
/// </summary>
public interface ILogFactory
{
/// <summary>
/// 创建日志操作
/// </summary>
/// <param name="categoryName">日志类别</param>
ILog CreateLog(string categoryName);

/// <summary>
/// 创建日志操作
/// </summary>
/// <param name="type">日志类别类型</param>
ILog CreateLog(Type type);
}
}
4 changes: 1 addition & 3 deletions framework/src/Bing.Logging/Bing/Logging/ILoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using Bing.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Bing.Logging
{
/// <summary>
/// 日志记录包装器
/// </summary>
/// <typeparam name="TCategoryName">日志类别</typeparam>
public interface ILoggerWrapper<out TCategoryName> : ITransientDependency
public interface ILoggerWrapper
{
/// <summary>
/// 是否启用
Expand Down
Loading

0 comments on commit ebac843

Please sign in to comment.