|
|
|
@ -15,6 +15,8 @@ using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using Serilog.Events;
|
|
|
|
|
using Serilog.Sinks.SystemConsole.Themes;
|
|
|
|
|
using SqlSugar.Extensions;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
|
|
|
@ -463,4 +465,48 @@ public static class ServiceCollectionExtensions
|
|
|
|
|
services.ConfigureOptions<ConfigureSwaggerOptions>();
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddDefaultSerilog(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
|
var serilogOptions = configuration.GetSection(SerilogOptions.Name).Get<SerilogOptions>();
|
|
|
|
|
if (serilogOptions is null || !serilogOptions.IsEnable)
|
|
|
|
|
{
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var loggerConfiguration = new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Information()
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
|
|
|
|
|
.Enrich.FromLogContext();
|
|
|
|
|
|
|
|
|
|
if (serilogOptions.WriteFile)
|
|
|
|
|
{
|
|
|
|
|
loggerConfiguration =
|
|
|
|
|
loggerConfiguration.WriteTo.File(Path.Combine("logs", "log"), rollingInterval: RollingInterval.Hour);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serilogOptions.SeqOptions?.IsEnable ?? false)
|
|
|
|
|
{
|
|
|
|
|
var serverUrl = configuration["SEQ_URL"] ?? serilogOptions.SeqOptions.Address;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(serverUrl);
|
|
|
|
|
var apiKey = configuration["SEQ_APIKEY"] ?? serilogOptions.SeqOptions.Secret;
|
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(apiKey);
|
|
|
|
|
loggerConfiguration = loggerConfiguration.WriteTo.Seq(
|
|
|
|
|
configuration["SEQ_URL"] ?? serilogOptions.SeqOptions.Address,
|
|
|
|
|
apiKey: configuration["SEQ_APIKEY"] ?? serilogOptions.SeqOptions.Secret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Logger = loggerConfiguration.CreateLogger();
|
|
|
|
|
services.AddLogging(logBuilder =>
|
|
|
|
|
{
|
|
|
|
|
logBuilder.ClearProviders();
|
|
|
|
|
logBuilder.AddSerilog(Log.Logger);
|
|
|
|
|
});
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|