123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- 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>(TState state) => null;
- public bool IsEnabled(LogLevel logLevel) => true;
- public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> 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);
- }
- }
- }
- }
- }
- }
|