init
This commit is contained in:
52
DouyinApi.Extensions/ServiceExtensions/SerilogSetup.cs
Normal file
52
DouyinApi.Extensions/ServiceExtensions/SerilogSetup.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.Common.LogHelper;
|
||||
using DouyinApi.Serilog.Configuration;
|
||||
using DouyinApi.Serilog.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using Serilog.Debugging;
|
||||
using Serilog.Events;
|
||||
using DouyinApi.Common.Option;
|
||||
|
||||
namespace DouyinApi.Extensions.ServiceExtensions;
|
||||
|
||||
public static class SerilogSetup
|
||||
{
|
||||
public static IHostBuilder AddSerilogSetup(this IHostBuilder host)
|
||||
{
|
||||
if (host == null) throw new ArgumentNullException(nameof(host));
|
||||
|
||||
var loggerConfiguration = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(AppSettings.Configuration)
|
||||
.Enrich.FromLogContext()
|
||||
//输出到控制台
|
||||
.WriteToConsole()
|
||||
//将日志保存到文件中
|
||||
.WriteToFile()
|
||||
//配置日志库
|
||||
.WriteToLogBatching();
|
||||
|
||||
var option = App.GetOptions<SeqOptions>();
|
||||
//配置Seq日志中心
|
||||
if (option.Enabled)
|
||||
{
|
||||
var address = option.Address;
|
||||
var apiKey = option.ApiKey;
|
||||
if (!address.IsNullOrEmpty())
|
||||
{
|
||||
loggerConfiguration =
|
||||
loggerConfiguration.WriteTo.Seq(address, restrictedToMinimumLevel: LogEventLevel.Verbose,
|
||||
apiKey: apiKey, eventBodyLimitBytes: 10485760);
|
||||
}
|
||||
}
|
||||
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
|
||||
//Serilog 内部日志
|
||||
var file = File.CreateText(LogContextStatic.Combine($"SerilogDebug{DateTime.Now:yyyyMMdd}.txt"));
|
||||
SelfLog.Enable(TextWriter.Synchronized(file));
|
||||
|
||||
host.UseSerilog();
|
||||
return host;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user