첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ TimeHelper.cs

using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;

namespace TestProject
{
    /// <summary>
    /// 시간 헬퍼
    /// </summary>
    public sealed class TimeHelper : IDisposable
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 로그 기록기
        /// </summary>
        private ILogger<Startup> logger;

        /// <summary>
        /// 메시지
        /// </summary>
        private string message;

        /// <summary>
        /// 스톱워치
        /// </summary>
        private Stopwatch stopwatch;

        /// <summary>
        /// 리소스 해제 여부
        /// </summary>
        private bool disposed = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - TimeHelper(logger, message)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="logger">로그 기록기</param>
        /// <param name="message">메시지</param>
        public TimeHelper(ILogger<Startup> logger, string message)
        {
            this.logger    = logger;
            this.message   = message;
            this.stopwatch = Stopwatch.StartNew();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 리소스 해제하기 - Dispose()

        /// <summary>
        /// 리소스 해제하기
        /// </summary>
        public void Dispose()
        {
            if(!this.disposed)
            {
                this.logger.LogInformation($"{message } : {stopwatch.ElapsedMilliseconds}ms");

                this.disposed = true;
            }
        }

        #endregion
    }
}

 

728x90

 

▶ Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

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.AddAuthorization();
        }

        #endregion
        #region 구성하기 - Configure(app, logger)

        /// <summary>
        /// 구성하기
        /// </summary>
        /// <param name="app">애플리케이션 빌더</param>
        /// <param name="logger">로그 기록기</param>
        public void Configure(IApplicationBuilder app, ILogger<Startup> logger)
        {
            int count = 0;

            // 인라인 미들웨어 1
            app.Use
            (
                next => async context =>
                {
                    using(new TimeHelper(logger, $"TIME {++count}"))
                    {
                        await next(context);
                    }
                }
            );

            app.UseRouting();

            // 인라인 미들웨어 2
            app.Use
            (
                next => async context =>
                {
                    using(new TimeHelper(logger, $"TIME {++count}"))
                    {
                        await next(context);
                    }
                }
            );

            app.UseAuthorization();

            // 인라인 미들웨어 3
            app.Use
            (
                next => async context =>
                {
                    using(new TimeHelper(logger, $"TIME {++count}"))
                    {
                        await next(context);
                    }
                }
            );

            app.UseEndpoints
            (
                endpoints =>
                {
                    endpoints.MapGet
                    (
                        "/",
                        async context =>
                        {
                            await context.Response.WriteAsync("Timing test.");
                        }
                    );
                }
            );
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요