1. Nuget 패키지를 설치한다.

2. NLog.Config 파일을 만들어 줍니다.
아무데나 원하는 곳에 NLog.Config 파일을 만들어 줍니다.

파일 내용은 이렇게 적습니다.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" />
<target name="consoleSimple" xsi:type="Console" layout="${message} ${exception}" />
<target name="coloredConsole" xsi:type="ColoredConsole" layout="[${level}] ${logger} | ${message} ${exception}" />
<target name="blackhole" xsi:type="Null" />
<target name="file" xsi:type="File" fileName="./logs/${shortdate}.log" layout="${longdate} ${level} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="coloredConsole" />
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
3. 코드 작성
이제 코드를 아래와 같이 작성합니다.
path는 위에서 만들어 준 NLog.Config 의 절대 경로를 입력했습니다.
프로세스를 실행하면 로그가 파일에 저장됩니다.
public class Program
{
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
string path = "/Users/jaeeun/free/TestSolution/NLog.config";
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(path);
log.Trace("test trace log");
log.Debug("test debug log");
log.Info("test info log");
log.Error("test error log");
log.Fatal("test fatal log");
log.Warn("test warn log");
}
}
4. 로그 파일 위치
로그 파일은 여기에 저장됩니다.
NLog.Config 파일의 fileName 으로 경로를 변경할 수 있습니다.

끝
'Server > C#' 카테고리의 다른 글
[C#] ??= (0) | 2023.06.20 |
---|---|
[C#] FirstOrDefault (0) | 2023.06.20 |
C# 델리게이트 (0) | 2023.06.09 |
static 맴버를 초기화하는 법 (정적 생성자) (0) | 2023.05.22 |
C# 서버 개발자 준비 (0) | 2023.05.15 |
1. Nuget 패키지를 설치한다.

2. NLog.Config 파일을 만들어 줍니다.
아무데나 원하는 곳에 NLog.Config 파일을 만들어 줍니다.

파일 내용은 이렇게 적습니다.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" />
<target name="consoleSimple" xsi:type="Console" layout="${message} ${exception}" />
<target name="coloredConsole" xsi:type="ColoredConsole" layout="[${level}] ${logger} | ${message} ${exception}" />
<target name="blackhole" xsi:type="Null" />
<target name="file" xsi:type="File" fileName="./logs/${shortdate}.log" layout="${longdate} ${level} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="coloredConsole" />
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
3. 코드 작성
이제 코드를 아래와 같이 작성합니다.
path는 위에서 만들어 준 NLog.Config 의 절대 경로를 입력했습니다.
프로세스를 실행하면 로그가 파일에 저장됩니다.
public class Program
{
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
string path = "/Users/jaeeun/free/TestSolution/NLog.config";
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(path);
log.Trace("test trace log");
log.Debug("test debug log");
log.Info("test info log");
log.Error("test error log");
log.Fatal("test fatal log");
log.Warn("test warn log");
}
}
4. 로그 파일 위치
로그 파일은 여기에 저장됩니다.
NLog.Config 파일의 fileName 으로 경로를 변경할 수 있습니다.

끝
'Server > C#' 카테고리의 다른 글
[C#] ??= (0) | 2023.06.20 |
---|---|
[C#] FirstOrDefault (0) | 2023.06.20 |
C# 델리게이트 (0) | 2023.06.09 |
static 맴버를 초기화하는 법 (정적 생성자) (0) | 2023.05.22 |
C# 서버 개발자 준비 (0) | 2023.05.15 |