using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DW5S.Repostory { public class SqlLoggerProvider : ILoggerProvider { public ILogger CreateLogger(string categoryName) => new SqlLogger(categoryName); public void Dispose() { } } public class SqlLogger : ILogger { private readonly string categoryName; public SqlLogger(string categoryName) => this.categoryName = categoryName; public IDisposable BeginScope(TState state) => null; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { if (exception != null) Serilog.Log.Error(exception, formatter(state, exception)); else { if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command") { if (logLevel == LogLevel.Warning) { var logContent = formatter(state, exception); Serilog.Log.Warning(exception, logContent); } else if (logLevel == LogLevel.Error) { var logContent = formatter(state, exception); Serilog.Log.Error(exception, logContent); } else { var logContent = formatter(state, exception); Serilog.Log.Information(logContent); } } } } } }