728x90
728x170

> Serilog 적용하기

1. 콘솔 프로젝트 생성(.NET 6.0)

2. Nuget 설치
   Serilog.Sinks.Console
   Serilog.Sinks.File

3. Log 설정

using Serilog;
using Serilog.Core;

namespace SerilogTest;

class Program
{
    static void Main(string[] args)
    {
        var levelSwitch = new LoggingLevelSwitch();

        Log.Logger = new LoggerConfiguration()
                    // 최소 지정 로그 레벨 : Info 레벨 이상 로그를 기록한다는 의미
                    .MinimumLevel.Information()
                    // 콘솔에도 내용을 남김
                    .WriteTo.Console()
                    //파일로 기록할 로그 파일명을 입력
                    .WriteTo.File(@"c:\log\log.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
                    .CreateLogger();

        for (int idx = 0; idx < 100; idx++)
        {
            Log.Information($"{idx} - Serilog Test");
        }

        Log.CloseAndFlush();
    }
}

4. 결과


> Seq 연동하기

1. Seq 설치
https://datalust.co/download

 

Seq — centralized structured logs for .NET, Java, Node.js

Seq is the intelligent search, analysis, and alerting server built specifically for modern structured log data.

datalust.co

2.  설치 후 관리자 설정

Browse Seq 버튼을 클릭하면 웹이사이트가 열리고 아까 지정한 아이디와 비밀번호를 입력한다.

3. Seq 설정을 끝났고 프로젝트에 Nuget 설치
  Serilog.Sinks.Seq

4. Serilog 를 이용해 Seq 연동
.WriteTo.Seq("http://localhost:5341") 코드를 추가하면된다.

using Serilog;
using Serilog.Core;

namespace SerilogTest;

class Program
{
    static void Main(string[] args)
    {
        var levelSwitch = new LoggingLevelSwitch();

        Log.Logger = new LoggerConfiguration()
                    // 최소 지정 로그 레벨 : Info 레벨 이상 로그를 기록한다는 의미
                    .MinimumLevel.Information()
                    // 콘솔에도 내용을 남김
                    .WriteTo.Console()
                    //파일로 기록할 로그 파일명을 입력
                    .WriteTo.File(@"c:\log\log.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true)
                    // Seq 에 로그 정보 입력
                    .WriteTo.Seq("http://localhost:5341", controlLevelSwitch: levelSwitch)
                    .CreateLogger();

        for (int idx = 0; idx < 100; idx++)
        {
            Log.Information($"{idx} - Serilog Test");
        }

        Log.CloseAndFlush();
    }
}

프로그램을 실행하고 웹사이트를 리프레쉬하면 아래처럼 로그 내용이 보여지게된다.

 

소스

https://github.com/kei-soft/KJunBlog/tree/master/SerilogTest

 

GitHub - kei-soft/KJunBlog

Contribute to kei-soft/KJunBlog development by creating an account on GitHub.

github.com

 

728x90
그리드형
Posted by kjun
,