■ ILoggerProvider 인터페이스에서 커스텀 로그 기록기 공급자를 사용하는 방법을 보여준다.
▶ Log/ColorConsoleLoggerConfiguration.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using Microsoft.Extensions.Logging; using System; namespace TestProject.Log { /// <summary> /// 색상 콘솔 로그 기록기 구성 /// </summary> public class ColorConsoleLoggerConfiguration { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 로그 레벨 - LogLevel /// <summary> /// 로그 레벨 /// </summary> public LogLevel LogLevel { get; set; } = LogLevel.Warning; #endregion #region 이벤트 ID - EventID /// <summary> /// 이벤트 ID /// </summary> public int EventID { get; set; } = 0; #endregion #region 색상 - Color /// <summary> /// 색성 /// </summary> public ConsoleColor Color { get; set; } = ConsoleColor.Yellow; #endregion } } |
▶ Log/ColorConsoleLogger.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
using Microsoft.Extensions.Logging; using System; namespace TestProject.Log { /// <summary> /// 색상 콘솔 로그 기록기 /// </summary> public class ColorConsoleLogger : ILogger { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 명칭 /// </summary> private readonly string name; /// <summary> /// 구성 /// </summary> private readonly ColorConsoleLoggerConfiguration configuration; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ColorConsoleLogger(name, configuration) /// <summary> /// 생성자 /// </summary> /// <param name="name">명칭</param> /// <param name="configuration">구성</param> public ColorConsoleLogger(string name, ColorConsoleLoggerConfiguration configuration) { this.name = name; this.configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 범위 시작하기 - BeginScope<TState>(state) /// <summary> /// 범위 시작하기 /// </summary> /// <typeparam name="TState">상태 타입</typeparam> /// <param name="state">상태</param> /// <returns>리소스 해제 인터페이스</returns> public IDisposable BeginScope<TState>(TState state) { return null; } #endregion #region 이용 가능 여부 구하기 - IsEnabled(logLevel) /// <summary> /// 이용 가능 여부 구하기 /// </summary> /// <param name="logLevel">로그 레벨</param> /// <returns>이용 가능 여부</returns> public bool IsEnabled(LogLevel logLevel) { return logLevel == this.configuration.LogLevel; } #endregion #region 로그 기록하기 - Log<TState>(logLevel, eventID, state, exception, formatter) /// <summary> /// 로그 기록하기 /// </summary> /// <typeparam name="TState">상태 타입</typeparam> /// <param name="logLevel">로그 레벨</param> /// <param name="eventID">이벤트 ID</param> /// <param name="state">상태</param> /// <param name="exception">예외</param> /// <param name="formatter">포매터</param> public void Log<TState> ( LogLevel logLevel, EventId eventID, TState state, Exception exception, Func<TState, Exception, string> formatter ) { if(!IsEnabled(logLevel)) { return; } if(this.configuration.EventID == 0 || this.configuration.EventID == eventID.Id) { ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = this.configuration.Color; Console.WriteLine($"{logLevel} - {eventID.Id} " + $"- {this.name} - {formatter(state, exception)}"); Console.ForegroundColor = color; } } #endregion } } |
▶ Log/ColorConsoleLoggerProvider.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using Microsoft.Extensions.Logging; using System.Collections.Concurrent; namespace TestProject.Log { /// <summary> /// 색상 콘솔 로기 기록기 제공자 /// </summary> public class ColorConsoleLoggerProvider : ILoggerProvider { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private readonly ColorConsoleLoggerConfiguration configuration; /// <summary> /// 로그 기록기 딕셔너리 /// </summary> private readonly ConcurrentDictionary<string, ColorConsoleLogger> dictionary = new ConcurrentDictionary<string, ColorConsoleLogger>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ColorConsoleLoggerProvider(configuration) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> public ColorConsoleLoggerProvider(ColorConsoleLoggerConfiguration configuration) { this.configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 로그 기록기 생성하기 - CreateLogger(categoryName) /// <summary> /// 로그 기록기 생성하기 /// </summary> /// <param name="categoryName">카테고리명</param> /// <returns>로그 기록기</returns> public ILogger CreateLogger(string categoryName) { return this.dictionary.GetOrAdd(categoryName, name => new ColorConsoleLogger(name, this.configuration)); } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { this.dictionary.Clear(); } #endregion } } |
▶ Log/ColorConsoleLoggerExtension.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
using Microsoft.Extensions.Logging; using System; namespace TestProject.Log { /// <summary> /// 색상 콘솔 로그 기록기 확장 /// </summary> public static class ColorConsoleLoggerExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 색상 콘솔 로그 기록기 추가하기 - AddColorConsoleLogger(loggerFactory, configuration) /// <summary> /// 색상 콘솔 로그 기록기 추가하기 /// </summary> /// <param name="loggerFactory">로그 기록기 팩토리</param> /// <param name="configuration">구성</param> /// <returns>로그 기록기 팩토리</returns> public static ILoggerFactory AddColorConsoleLogger ( this ILoggerFactory loggerFactory, ColorConsoleLoggerConfiguration configuration ) { loggerFactory.AddProvider(new ColorConsoleLoggerProvider(configuration)); return loggerFactory; } #endregion #region 색상 콘솔 로그 기록기 추가하기 - AddColorConsoleLogger(loggerFactory) /// <summary> /// 색상 콘솔 로그 기록기 추가하기 /// </summary> /// <param name="loggerFactory">로그 기록기 팩토리</param> /// <returns>로그 기록기 팩토리</returns> public static ILoggerFactory AddColorConsoleLogger(this ILoggerFactory loggerFactory) { ColorConsoleLoggerConfiguration configuration = new ColorConsoleLoggerConfiguration(); return loggerFactory.AddColorConsoleLogger(configuration); } #endregion #region 색상 콘솔 로그 기록기 추가하기 - AddColorConsoleLogger(loggerFactory, configure) /// <summary> /// 색상 콘솔 로그 기록기 추가하기 /// </summary> /// <param name="loggerFactory">로그 기록기 팩토리</param> /// <param name="configure">구성 액션</param> /// <returns>로그 기록기 팩토리</returns> public static ILoggerFactory AddColorConsoleLogger ( this ILoggerFactory loggerFactory, Action<ColorConsoleLoggerConfiguration> configure ) { ColorConsoleLoggerConfiguration configuration = new ColorConsoleLoggerConfiguration(); configure(configuration); return loggerFactory.AddColorConsoleLogger(configuration); } #endregion } } |
▶ Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using TestProject.Log; namespace TestProject { /// <summary> /// 시작 /// </summary> public class Startup { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 구성 - Configuration /// <summary> /// 구성 /// </summary> public IConfiguration Configuration { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Startup(configuration) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> public Startup(IConfiguration configuration) { Configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스 컬렉션 구성하기 - ConfigureServices(services) /// <summary> /// 서비스 컬렉션 구성하기 /// </summary> /// <param name="services">서비스 컬렉션</param> public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } #endregion #region 구성하기 - Configure(app, environment) /// <summary> /// 구성하기 /// </summary> /// <param name="app">애플리케이션 빌더</param> /// <param name="environment">웹 호스트 환경</param> /// <param name="loggerFactory">로그 기록기 팩토리</param> public void Configure(IApplicationBuilder app, IWebHostEnvironment environment, ILoggerFactory loggerFactory) { loggerFactory.AddProvider ( new ColorConsoleLoggerProvider ( new ColorConsoleLoggerConfiguration { LogLevel = LogLevel.Error, Color = ConsoleColor.Red } ) ); loggerFactory.AddColorConsoleLogger(); loggerFactory.AddColorConsoleLogger ( new ColorConsoleLoggerConfiguration { LogLevel = LogLevel.Debug, Color = ConsoleColor.Gray } ); loggerFactory.AddColorConsoleLogger ( configuration => { configuration.LogLevel = LogLevel.Information; configuration.Color = ConsoleColor.Blue; } ); if(environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints ( endpoints => { endpoints.MapControllerRoute ( name : "default", pattern : "{controller=Home}/{action=Index}/{id?}" ); } ); } #endregion } } |