You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.6 KiB
51 lines
1.6 KiB
using Microsoft.Extensions.Hosting;
|
|
using Serilog;
|
|
using Serilog.Debugging;
|
|
using Serilog.Events;
|
|
using Tiobon.Core.Common.LogHelper;
|
|
using Tiobon.Core.Option;
|
|
using Tiobon.Core.Serilog.Configuration;
|
|
using Tiobon.Core.Serilog.Extensions;
|
|
|
|
namespace Tiobon.Core.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;
|
|
}
|
|
} |