You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB
C#

using EntrustSettle.Common;
using EntrustSettle.Common.LogHelper;
using EntrustSettle.Serilog.Configuration;
using EntrustSettle.Serilog.Extensions;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Debugging;
using System;
using System.IO;
namespace EntrustSettle.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();
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;
}
}