feat: initialize Evergreen Health backend
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库日志写入器
|
||||
/// </summary>
|
||||
public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
||||
{
|
||||
private readonly IServiceScope _serviceScope;
|
||||
private readonly ISqlSugarClient _db;
|
||||
private readonly SysConfigService _sysConfigService; // 参数配置服务
|
||||
private readonly ILogger<DatabaseLoggingWriter> _logger; // 日志组件
|
||||
|
||||
public DatabaseLoggingWriter(IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_serviceScope = scopeFactory.CreateScope();
|
||||
//_db = _serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
|
||||
_sysConfigService = _serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
|
||||
_logger = _serviceScope.ServiceProvider.GetRequiredService<ILogger<DatabaseLoggingWriter>>();
|
||||
|
||||
// 切换日志独立数据库
|
||||
_db = SqlSugarSetup.ITenant.IsAnyConnection(SqlSugarConst.LogConfigId)
|
||||
? SqlSugarSetup.ITenant.GetConnectionScope(SqlSugarConst.LogConfigId)
|
||||
: SqlSugarSetup.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
|
||||
}
|
||||
|
||||
public async Task WriteAsync(LogMessage logMsg, bool flush)
|
||||
{
|
||||
var jsonStr = logMsg.Context?.Get("loggingMonitor")?.ToString();
|
||||
if (string.IsNullOrWhiteSpace(jsonStr))
|
||||
{
|
||||
await _db.Insertable(new SysLogOp
|
||||
{
|
||||
DisplayTitle = "自定义操作日志",
|
||||
LogDateTime = logMsg.LogDateTime,
|
||||
EventId = logMsg.EventId.Id,
|
||||
ThreadId = logMsg.ThreadId,
|
||||
TraceId = logMsg.TraceId,
|
||||
Exception = logMsg.Exception == null ? null : JSON.Serialize(logMsg.Exception),
|
||||
Message = logMsg.Message,
|
||||
LogLevel = logMsg.LogLevel,
|
||||
Status = "200",
|
||||
}).ExecuteCommandAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
var loggingMonitor = JSON.Deserialize<dynamic>(jsonStr);
|
||||
// 记录数据校验日志
|
||||
if (loggingMonitor.validation != null && !await _sysConfigService.GetConfigValue<bool>(ConfigConst.SysValidationLog)) return;
|
||||
|
||||
// 获取当前操作者
|
||||
string account = "", realName = "", userId = "", tenantId = "";
|
||||
if (loggingMonitor.authorizationClaims != null)
|
||||
{
|
||||
var map = (loggingMonitor.authorizationClaims as IEnumerable<dynamic>)
|
||||
!.ToDictionary(u => u.type.ToString(), u => u.value.ToString());
|
||||
account = map.GetValueOrDefault(ClaimConst.Account);
|
||||
realName = map.GetValueOrDefault(ClaimConst.RealName);
|
||||
tenantId = map.GetValueOrDefault(ClaimConst.TenantId);
|
||||
userId = map.GetValueOrDefault(ClaimConst.UserId);
|
||||
}
|
||||
|
||||
// 优先获取 X-Forwarded-For 头部信息携带的IP地址(如nginx代理配置转发)
|
||||
var remoteIPv4 = ((JArray)loggingMonitor.requestHeaders).OfType<JObject>()
|
||||
.FirstOrDefault(header => (string)header["key"] == "X-Forwarded-For")?["value"]?.ToString();
|
||||
|
||||
if (string.IsNullOrEmpty(remoteIPv4))
|
||||
remoteIPv4 = loggingMonitor.remoteIPv4;
|
||||
|
||||
remoteIPv4 = remoteIPv4?.Split(',')?.FirstOrDefault()?.Trim();
|
||||
|
||||
(string ipLocation, double? longitude, double? latitude) = CommonUtil.GetIpAddress(remoteIPv4);
|
||||
|
||||
var browser = "";
|
||||
var os = "";
|
||||
if (loggingMonitor.userAgent != null)
|
||||
{
|
||||
var client = Parser.GetDefault().Parse(loggingMonitor.userAgent.ToString());
|
||||
browser = $"{client.UA.Family} {client.UA.Major}.{client.UA.Minor} / {client.Device.Family}";
|
||||
os = $"{client.OS.Family} {client.OS.Major} {client.OS.Minor}";
|
||||
}
|
||||
|
||||
// 捕捉异常,否则会由于 unhandled exception 导致程序崩溃
|
||||
try
|
||||
{
|
||||
// 记录异常日志-发送邮件
|
||||
if (logMsg.Exception != null || loggingMonitor.exception != null)
|
||||
{
|
||||
await _db.Insertable(new SysLogEx
|
||||
{
|
||||
ControllerName = loggingMonitor.controllerName,
|
||||
ActionName = loggingMonitor.actionTypeName,
|
||||
DisplayTitle = loggingMonitor.displayTitle,
|
||||
Status = loggingMonitor.returnInformation?.httpStatusCode,
|
||||
RemoteIp = remoteIPv4,
|
||||
Location = ipLocation,
|
||||
Longitude = (decimal?)longitude,
|
||||
Latitude = (decimal?)latitude,
|
||||
Browser = browser, // loggingMonitor.userAgent,
|
||||
Os = os, // loggingMonitor.osDescription + " " + loggingMonitor.osArchitecture,
|
||||
Elapsed = loggingMonitor.timeOperationElapsedMilliseconds,
|
||||
LogDateTime = logMsg.LogDateTime,
|
||||
Account = account,
|
||||
RealName = realName,
|
||||
HttpMethod = loggingMonitor.httpMethod,
|
||||
RequestUrl = loggingMonitor.requestUrl,
|
||||
RequestParam = (loggingMonitor.parameters == null || loggingMonitor.parameters.Count == 0) ? null : JSON.Serialize(loggingMonitor.parameters[0].value),
|
||||
ReturnResult = loggingMonitor.returnInformation == null ? null : JSON.Serialize(loggingMonitor.returnInformation),
|
||||
EventId = logMsg.EventId.Id,
|
||||
ThreadId = logMsg.ThreadId,
|
||||
TraceId = logMsg.TraceId,
|
||||
Exception = JSON.Serialize(loggingMonitor.exception),
|
||||
Message = logMsg.Message,
|
||||
CreateUserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
|
||||
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : long.Parse(tenantId),
|
||||
LogLevel = logMsg.LogLevel
|
||||
}).ExecuteCommandAsync();
|
||||
|
||||
// 将异常日志发送到邮件
|
||||
if (await _sysConfigService.GetConfigValue<bool>(ConfigConst.SysErrorMail))
|
||||
{
|
||||
await App.GetRequiredService<IEventPublisher>().PublishAsync(CommonConst.SendErrorMail, logMsg.Exception ?? loggingMonitor.exception);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 记录访问日志-登录退出
|
||||
if (loggingMonitor.actionName == "userInfo" || loggingMonitor.actionName == "logout")
|
||||
{
|
||||
await _db.Insertable(new SysLogVis
|
||||
{
|
||||
ControllerName = loggingMonitor.controllerName,
|
||||
ActionName = loggingMonitor.actionTypeName,
|
||||
DisplayTitle = loggingMonitor.displayTitle,
|
||||
Status = loggingMonitor.returnInformation?.httpStatusCode,
|
||||
RemoteIp = remoteIPv4,
|
||||
Location = ipLocation,
|
||||
Longitude = (decimal?)longitude,
|
||||
Latitude = (decimal?)latitude,
|
||||
Browser = browser, // loggingMonitor.userAgent,
|
||||
Os = os, // loggingMonitor.osDescription + " " + loggingMonitor.osArchitecture,
|
||||
Elapsed = loggingMonitor.timeOperationElapsedMilliseconds,
|
||||
LogDateTime = logMsg.LogDateTime,
|
||||
Account = account,
|
||||
RealName = realName,
|
||||
CreateUserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
|
||||
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : long.Parse(tenantId),
|
||||
LogLevel = logMsg.LogLevel
|
||||
}).ExecuteCommandAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
if (!await _sysConfigService.GetConfigValue<bool>(ConfigConst.SysOpLog)) return;
|
||||
await _db.Insertable(new SysLogOp
|
||||
{
|
||||
ControllerName = loggingMonitor.controllerName,
|
||||
ActionName = loggingMonitor.actionTypeName,
|
||||
DisplayTitle = loggingMonitor.displayTitle,
|
||||
Status = loggingMonitor.returnInformation?.httpStatusCode,
|
||||
RemoteIp = remoteIPv4,
|
||||
Location = ipLocation,
|
||||
Longitude = (decimal?)longitude,
|
||||
Latitude = (decimal?)latitude,
|
||||
Browser = browser, // loggingMonitor.userAgent,
|
||||
Os = os, // loggingMonitor.osDescription + " " + loggingMonitor.osArchitecture,
|
||||
Elapsed = loggingMonitor.timeOperationElapsedMilliseconds,
|
||||
LogDateTime = logMsg.LogDateTime,
|
||||
Account = account,
|
||||
RealName = realName,
|
||||
HttpMethod = loggingMonitor.httpMethod,
|
||||
RequestUrl = loggingMonitor.requestUrl,
|
||||
RequestParam = (loggingMonitor.parameters == null || loggingMonitor.parameters.Count == 0) ? null : JSON.Serialize(loggingMonitor.parameters[0].value),
|
||||
ReturnResult = loggingMonitor.returnInformation == null ? null : JSON.Serialize(loggingMonitor.returnInformation),
|
||||
EventId = logMsg.EventId.Id,
|
||||
ThreadId = logMsg.ThreadId,
|
||||
TraceId = logMsg.TraceId,
|
||||
Exception = loggingMonitor.exception == null ? null : JSON.Serialize(loggingMonitor.exception),
|
||||
Message = logMsg.Message,
|
||||
CreateUserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
|
||||
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : long.Parse(tenantId),
|
||||
LogLevel = logMsg.LogLevel
|
||||
}).ExecuteCommandAsync();
|
||||
|
||||
await Task.Delay(50); // 延迟 0.05 秒写入数据库,有效减少高频写入数据库导致死锁问题
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "操作日志入库");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放服务作用域
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_serviceScope.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using Elastic.Clients.Elasticsearch;
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// ES日志写入器
|
||||
/// </summary>
|
||||
public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable
|
||||
{
|
||||
private readonly IServiceScope _serviceScope;
|
||||
private readonly ElasticsearchClient _esClient;
|
||||
private readonly SysConfigService _sysConfigService;
|
||||
|
||||
public ElasticSearchLoggingWriter(IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_serviceScope = scopeFactory.CreateScope();
|
||||
_esClient = _serviceScope.ServiceProvider.GetRequiredService<ElasticSearchClientContainer>()?.Logging;
|
||||
_sysConfigService = _serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
|
||||
}
|
||||
|
||||
public async Task WriteAsync(LogMessage logMsg, bool flush)
|
||||
{
|
||||
// 是否启用操作日志
|
||||
var sysOpLogEnabled = await _sysConfigService.GetConfigValue<bool>(ConfigConst.SysOpLog);
|
||||
if (!sysOpLogEnabled) return;
|
||||
|
||||
var jsonStr = logMsg.Context?.Get("loggingMonitor")?.ToString();
|
||||
if (string.IsNullOrWhiteSpace(jsonStr)) return;
|
||||
|
||||
var loggingMonitor = JSON.Deserialize<dynamic>(jsonStr);
|
||||
|
||||
// 不记录登录退出日志
|
||||
if (loggingMonitor.actionName == "userInfo" || loggingMonitor.actionName == "logout")
|
||||
return;
|
||||
|
||||
// 获取当前操作者
|
||||
string account = "", realName = "", userId = "", tenantId = "";
|
||||
if (loggingMonitor.authorizationClaims != null)
|
||||
{
|
||||
foreach (var item in loggingMonitor.authorizationClaims)
|
||||
{
|
||||
if (item.type == ClaimConst.Account)
|
||||
account = item.value;
|
||||
if (item.type == ClaimConst.RealName)
|
||||
realName = item.value;
|
||||
if (item.type == ClaimConst.TenantId)
|
||||
tenantId = item.value;
|
||||
if (item.type == ClaimConst.UserId)
|
||||
userId = item.value;
|
||||
}
|
||||
}
|
||||
|
||||
string remoteIPv4 = loggingMonitor.remoteIPv4;
|
||||
(string ipLocation, double? longitude, double? latitude) = CommonUtil.GetIpAddress(remoteIPv4);
|
||||
|
||||
var sysLogOp = new SysLogOp
|
||||
{
|
||||
Id = DateTime.Now.Ticks,
|
||||
ControllerName = loggingMonitor.controllerName,
|
||||
ActionName = loggingMonitor.actionTypeName,
|
||||
DisplayTitle = loggingMonitor.displayTitle,
|
||||
Status = loggingMonitor.returnInformation.httpStatusCode,
|
||||
RemoteIp = remoteIPv4,
|
||||
Location = ipLocation,
|
||||
Longitude = (decimal?)longitude,
|
||||
Latitude = (decimal?)latitude,
|
||||
Browser = loggingMonitor.userAgent,
|
||||
Os = loggingMonitor.osDescription + " " + loggingMonitor.osArchitecture,
|
||||
Elapsed = loggingMonitor.timeOperationElapsedMilliseconds,
|
||||
LogDateTime = logMsg.LogDateTime,
|
||||
Account = account,
|
||||
RealName = realName,
|
||||
HttpMethod = loggingMonitor.httpMethod,
|
||||
RequestUrl = loggingMonitor.requestUrl,
|
||||
RequestParam = (loggingMonitor.parameters == null || loggingMonitor.parameters.Count == 0) ? null : JSON.Serialize(loggingMonitor.parameters[0].value),
|
||||
ReturnResult = JSON.Serialize(loggingMonitor.returnInformation),
|
||||
EventId = logMsg.EventId.Id,
|
||||
ThreadId = logMsg.ThreadId,
|
||||
TraceId = logMsg.TraceId,
|
||||
Exception = (loggingMonitor.exception == null) ? null : JSON.Serialize(loggingMonitor.exception),
|
||||
Message = logMsg.Message,
|
||||
CreateUserId = string.IsNullOrWhiteSpace(userId) ? 0 : long.Parse(userId),
|
||||
TenantId = string.IsNullOrWhiteSpace(tenantId) ? 0 : long.Parse(tenantId)
|
||||
};
|
||||
await _esClient.IndexAsync(sysLogOp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放服务作用域
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_serviceScope.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
//using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
//using System.Security.Claims;
|
||||
|
||||
//namespace Admin.NET.Core.Logging;
|
||||
|
||||
///// <summary>
|
||||
///// 全局异常处理
|
||||
///// </summary>
|
||||
//public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton
|
||||
//{
|
||||
// private readonly IEventPublisher _eventPublisher;
|
||||
|
||||
// public LogExceptionHandler(IEventPublisher eventPublisher)
|
||||
// {
|
||||
// _eventPublisher = eventPublisher;
|
||||
// }
|
||||
|
||||
// public async Task OnExceptionAsync(ExceptionContext context)
|
||||
// {
|
||||
// var actionMethod = (context.ActionDescriptor as ControllerActionDescriptor)?.MethodInfo;
|
||||
// var displayNameAttribute = actionMethod.IsDefined(typeof(DisplayNameAttribute), true) ? actionMethod.GetCustomAttribute<DisplayNameAttribute>(true) : default;
|
||||
|
||||
// var sysLogEx = new SysLogEx
|
||||
// {
|
||||
// Account = App.User?.FindFirstValue(ClaimConst.Account),
|
||||
// RealName = App.User?.FindFirstValue(ClaimConst.RealName),
|
||||
// ControllerName = actionMethod.DeclaringType.FullName,
|
||||
// ActionName = actionMethod.Name,
|
||||
// DisplayTitle = displayNameAttribute?.DisplayName,
|
||||
// Exception = $"异常信息:{context.Exception.Message} 异常来源:{context.Exception.Source} 堆栈信息:{context.Exception.StackTrace}",
|
||||
// Message = "全局异常",
|
||||
// RequestParam = context.Exception.TargetSite.GetParameters().ToString(),
|
||||
// HttpMethod = context.HttpContext.Request.Method,
|
||||
// RequestUrl = context.HttpContext.Request.GetRequestUrlAddress(),
|
||||
// RemoteIp = context.HttpContext.GetRemoteIpAddressToIPv4(),
|
||||
// Browser = context.HttpContext.Request.Headers["User-Agent"],
|
||||
// TraceId = App.GetTraceId(),
|
||||
// ThreadId = App.GetThreadId(),
|
||||
// LogDateTime = DateTime.Now,
|
||||
// LogLevel = LogLevel.Error
|
||||
// };
|
||||
|
||||
// await _eventPublisher.PublishAsync(new ChannelEventSource("Add:ExLog", sysLogEx));
|
||||
|
||||
// await _eventPublisher.PublishAsync("Send:ErrorMail", sysLogEx);
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,104 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
public static class LoggingSetup
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志注册
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
public static void AddLoggingSetup(this IServiceCollection services)
|
||||
{
|
||||
// 日志监听
|
||||
services.AddMonitorLogging(options =>
|
||||
{
|
||||
options.IgnorePropertyNames = new[] { "Byte" };
|
||||
options.IgnorePropertyTypes = new[] { typeof(byte[]) };
|
||||
});
|
||||
|
||||
// 控制台日志
|
||||
var consoleLog = App.GetConfig<bool>("Logging:Monitor:ConsoleLog", true);
|
||||
services.AddConsoleFormatter(options =>
|
||||
{
|
||||
options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
|
||||
//options.WithTraceId = true; // 显示线程Id
|
||||
//options.WithStackFrame = true; // 显示程序集
|
||||
options.WriteFilter = (logMsg) =>
|
||||
{
|
||||
return consoleLog;
|
||||
};
|
||||
});
|
||||
|
||||
// 日志写入文件
|
||||
if (App.GetConfig<bool>("Logging:File:Enabled", true))
|
||||
{
|
||||
var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);
|
||||
Array.ForEach(new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error }, logLevel =>
|
||||
{
|
||||
services.AddFileLogging(options =>
|
||||
{
|
||||
options.WithTraceId = true; // 显示线程Id
|
||||
options.WithStackFrame = true; // 显示程序集
|
||||
options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, logLevel.ToString()); // 每天创建一个文件
|
||||
options.WriteFilter = logMsg => logMsg.LogLevel == logLevel; // 日志级别
|
||||
options.HandleWriteError = (writeError) => // 写入失败时启用备用文件
|
||||
{
|
||||
writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
|
||||
};
|
||||
if (loggingMonitorSettings.JsonBehavior == JsonBehavior.OnlyJson)
|
||||
{
|
||||
options.MessageFormat = LoggerFormatter.Json;
|
||||
// options.MessageFormat = LoggerFormatter.JsonIndented;
|
||||
options.MessageFormat = (logMsg) =>
|
||||
{
|
||||
var jsonString = logMsg.Context.Get("loggingMonitor");
|
||||
return jsonString?.ToString();
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 日志写入ElasticSearch
|
||||
if (App.GetConfig<bool>("ElasticSearch:Logging:Enabled", true))
|
||||
{
|
||||
services.AddDatabaseLogging<ElasticSearchLoggingWriter>(options =>
|
||||
{
|
||||
options.WithTraceId = true; // 显示线程Id
|
||||
options.WithStackFrame = true; // 显示程序集
|
||||
options.IgnoreReferenceLoop = false; // 忽略循环检测
|
||||
options.MessageFormat = LoggerFormatter.Json;
|
||||
options.WriteFilter = (logMsg) =>
|
||||
{
|
||||
return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// 日志写入数据库
|
||||
if (App.GetConfig<bool>("Logging:Database:Enabled", true))
|
||||
{
|
||||
services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
|
||||
{
|
||||
options.WithTraceId = true; // 显示线程Id
|
||||
options.WithStackFrame = true; // 显示程序集
|
||||
options.IgnoreReferenceLoop = false; // 忽略循环检测
|
||||
options.MessageFormat = (logMsg) =>
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine(logMsg.Message);
|
||||
return stringBuilder.ToString();
|
||||
};
|
||||
options.WriteFilter = (logMsg) =>
|
||||
{
|
||||
return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user