mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-10-26 21:27:10 +08:00
142 lines
5.0 KiB
C#
142 lines
5.0 KiB
C#
// ------------------------------------------------------------------------
|
||
// 版权信息
|
||
// 版权归百小僧及百签科技(广东)有限公司所有。
|
||
// 所有权利保留。
|
||
// 官方网站:https://baiqian.com
|
||
//
|
||
// 许可证信息
|
||
// 项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。
|
||
// 许可证的完整文本可以在源代码树根目录中的 LICENSE-APACHE 和 LICENSE-MIT 文件中找到。
|
||
// ------------------------------------------------------------------------
|
||
|
||
using Microsoft.Extensions.Logging;
|
||
using Microsoft.Extensions.Logging.Abstractions;
|
||
using Microsoft.Extensions.Logging.Console;
|
||
using Microsoft.Extensions.Options;
|
||
|
||
namespace ThingsGateway.Logging;
|
||
|
||
/// <summary>
|
||
/// 控制台默认格式化程序拓展
|
||
/// </summary>
|
||
[SuppressSniffer]
|
||
public sealed class ConsoleFormatterExtend : ConsoleFormatter, IDisposable
|
||
{
|
||
/// <summary>
|
||
/// 日志格式化选项刷新 Token
|
||
/// </summary>
|
||
private readonly IDisposable _formatOptionsReloadToken;
|
||
|
||
/// <summary>
|
||
/// 日志格式化配置选项
|
||
/// </summary>
|
||
private ConsoleFormatterExtendOptions _formatterOptions;
|
||
|
||
/// <summary>
|
||
/// 是否启用控制台颜色
|
||
/// </summary>
|
||
private bool _disableColors;
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="formatterOptions"></param>
|
||
public ConsoleFormatterExtend(IOptionsMonitor<ConsoleFormatterExtendOptions> formatterOptions)
|
||
: base("console-format")
|
||
{
|
||
(_formatOptionsReloadToken, _formatterOptions) = (formatterOptions.OnChange(ReloadFormatterOptions), formatterOptions.CurrentValue);
|
||
_disableColors = _formatterOptions.ColorBehavior == LoggerColorBehavior.Disabled || (_formatterOptions.ColorBehavior == LoggerColorBehavior.Default && Console.IsOutputRedirected);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 写入日志
|
||
/// </summary>
|
||
/// <typeparam name="TState"></typeparam>
|
||
/// <param name="logEntry"></param>
|
||
/// <param name="scopeProvider"></param>
|
||
/// <param name="textWriter"></param>
|
||
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter)
|
||
{
|
||
// 获取格式化后的消息
|
||
var message = logEntry.Formatter?.Invoke(logEntry.State, logEntry.Exception);
|
||
|
||
// 日志消息内容转换(如脱敏处理)
|
||
if (_formatterOptions.MessageProcess != null)
|
||
{
|
||
message = _formatterOptions.MessageProcess(message);
|
||
}
|
||
|
||
// 创建日志消息
|
||
var logDateTime = _formatterOptions.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now;
|
||
var logMsg = new LogMessage(logEntry.Category, logEntry.LogLevel, logEntry.EventId, message, logEntry.Exception, null, logEntry.State, logDateTime, Environment.CurrentManagedThreadId, _formatterOptions.UseUtcTimestamp, App.GetTraceId())
|
||
{
|
||
// 设置日志上下文
|
||
Context = Penetrates.SetLogContext(scopeProvider, _formatterOptions.IncludeScopes)
|
||
};
|
||
|
||
string standardMessage;
|
||
|
||
// 是否自定义了自定义日志格式化程序,如果是则使用
|
||
if (_formatterOptions.MessageFormat != null)
|
||
{
|
||
// 设置日志消息模板
|
||
standardMessage = _formatterOptions.MessageFormat(logMsg);
|
||
}
|
||
else
|
||
{
|
||
// 获取标准化日志消息
|
||
standardMessage = Penetrates.OutputStandardMessage(logMsg
|
||
, _formatterOptions.DateFormat
|
||
, true
|
||
, _disableColors
|
||
, _formatterOptions.WithTraceId
|
||
, _formatterOptions.WithStackFrame
|
||
, _formatterOptions.FormatProvider);
|
||
}
|
||
|
||
// 判断是否自定义了日志筛选器,如果是则检查是否符合条件
|
||
if (_formatterOptions.WriteFilter?.Invoke(logMsg) == false)
|
||
{
|
||
logMsg.Context?.Dispose();
|
||
return;
|
||
}
|
||
|
||
// 空检查
|
||
if (message is null)
|
||
{
|
||
logMsg.Context?.Dispose();
|
||
return;
|
||
}
|
||
|
||
// 判断是否自定义了日志格式化程序
|
||
if (_formatterOptions.WriteHandler != null)
|
||
{
|
||
_formatterOptions.WriteHandler?.Invoke(logMsg, scopeProvider, textWriter, standardMessage, _formatterOptions);
|
||
}
|
||
else
|
||
{
|
||
// 写入控制台
|
||
textWriter.WriteLine(standardMessage);
|
||
}
|
||
|
||
logMsg.Context?.Dispose();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 释放非托管资源
|
||
/// </summary>
|
||
public void Dispose()
|
||
{
|
||
_formatOptionsReloadToken?.Dispose();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新日志格式化选项
|
||
/// </summary>
|
||
/// <param name="options"></param>
|
||
private void ReloadFormatterOptions(ConsoleFormatterExtendOptions options)
|
||
{
|
||
_formatterOptions = options;
|
||
_disableColors = options.ColorBehavior == LoggerColorBehavior.Disabled || (options.ColorBehavior == LoggerColorBehavior.Default && Console.IsOutputRedirected);
|
||
}
|
||
} |