๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์›น๊ณต๋ทฐ

ASP.NET core ํŒŒ์ผ ๋กœ๊น…ํ•˜๊ธฐ

by ์ด๋…ธํ‚ค_ 2021. 4. 9.

ํ”„๋กœ์ ํŠธ ํ…œํ”Œ๋ฆฟ์ด Web API, MVC ํ”„๋กœ์ ํŠธ๋“  ๋ญ๋“  ์ƒ๊ด€์—†๋‹ค.

 

1. Startup.cs ์ฝ”๋“œ ์ˆ˜์ •

(1) Configure()์— ILoggerFactory ํŒŒ๋ผ๋ฏธํ„ฐ ์ถ”๊ฐ€

(2) loggerFactory path ์„ค์ •

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
     }

    var _path = Directory.GetCurrentDirectory();
    loggerFactory.AddFile($"{_path}\\logs\\log.txt");
    ....
   }

(3) loggerFactory.AddFile() ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. 

์ด๋•Œ๋Š” nuget์—์„œ logging ๊ด€๋ จ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. 

ํ˜„์žฌ vsc์—์„œ ๊ฐœ๋ฐœ์ค‘์ด๋ฏ€๋กœ vsc์˜ ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํƒ€์ดํ•‘ํ•˜์—ฌ ์„ค์น˜ํ•œ๋‹ค.

dotnet add package Serilog.Extensions.Logging.File --version 2.0.0

 

NuGet Gallery | Serilog.Extensions.Logging.File 2.0.0

 

Serilog.Extensions.Logging.File 2.0.0

Add file logging to ASP.NET Core apps with one line of code.

www.nuget.org

(4) .AddFile() ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ฝ”๋”ฉ๋œ๋‹ค.

 

2. ๋กœ๊น…ํ•˜๋ ค๋Š” ํŒŒ์ผ๋กœ ์ด๋™

(1) logger๊ฐ์ฒด ์ƒ์„ฑํ›„, logger.~(๋กœ๊ทธ ๋ฉ”์†Œ๋“œ๋“ค)์„ ์ด์šฉํ•˜์—ฌ ๋กœ๊น…์„ ํ•œ๋‹ค.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace DNAPlus.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            _logger.LogInformation($"Execute time : {DateTime.Now}");

            var rng = new Random();
            return Enumerable.Range(1, 20).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

3. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  1.์—์„œ ์„ค์ •ํ•œ path์œ„์น˜๋กœ ๊ฐ€๋ฉด ๋กœ๊ทธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์–ด์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฐธ๊ณ  : Add File Logging to an ASP.NET Core MVC Application (c-sharpcorner.com)

 

Add File Logging to an ASP.NET Core MVC Application

One of the most common aspects of any application is the ability to write logs. These not only help to troubleshoot problems that arise out of an application but also help to keep track of how things are going and take pre-emptive measures to stop problems

www.c-sharpcorner.com

 

 

๋Œ“๊ธ€