using Microsoft.Extensions.Logging; namespace NCC.Logging { /// /// 构建字符串日志部分类 /// public sealed partial class StringLoggingPart { /// /// Information /// public void LogInformation() { SetLevel(LogLevel.Information).Log(); } /// /// Warning /// public void LogWarning() { SetLevel(LogLevel.Warning).Log(); } /// /// Error /// public void LogError() { SetLevel(LogLevel.Error).Log(); } /// /// Debug /// public void LogDebug() { SetLevel(LogLevel.Debug).Log(); } /// /// Trace /// public void LogTrace() { SetLevel(LogLevel.Trace).Log(); } /// /// Critical /// public void LogCritical() { SetLevel(LogLevel.Critical).Log(); } /// /// 写入日志 /// /// public void Log() { if (Message == null) return; var logger = !string.IsNullOrWhiteSpace(CategoryName) ? App.GetService(LoggerScoped ?? App.RootServices)?.CreateLogger(CategoryName) : App.GetService(typeof(ILogger<>).MakeGenericType(CategoryType), LoggerScoped ?? App.RootServices) as ILogger; // 如果没有异常且事件 Id 为空 if (Exception == null && EventId == null) { logger.Log(Level, Message, Args); } // 如果存在异常且事件 Id 为空 else if (Exception != null && EventId == null) { logger.Log(Level, Exception, Message, Args); } // 如果异常为空且事件 Id 不为空 else if (Exception == null && EventId != null) { logger.Log(Level, EventId.Value, Message, Args); } // 如果存在异常且事件 Id 不为空 else if (Exception != null && EventId != null) { logger.Log(Level, EventId.Value, Exception, Message, Args); } else { } } } }