본문 바로가기
[ Program ]/C#

[c#] Nlog 사용법

by 관이119 2022. 9. 23.

c# 에서 nlog 사용법을 올린다. ( https://nlog-project.org/ )

nuget 에서 nlog 검색해서 설치하면 되는데 설치하기 귀찬은사람은 첨부 파일을 받아서 참조만 추가해주면된다.

현재 최신버전인 5.0.4 버전이다.

nlog.zip
0.44MB

참조된후 아래 코드로 테스트 해보자.

 

 

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
using NLog;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace nlogTest
{
    public partial class Form1 : Form
    {
        public static string __LOG_Format = @"Logs\{0}\${{date:format=yyyyMMddHHmmss}}.log";
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            LoggerTestA _LogA = new LoggerTestA();
            LoggerTestB _LogB = new LoggerTestB();
 
            _LogA.write();
            _LogB.write();
            _LogA.write();
            _LogB.write();
            _LogA.write();
            _LogA.write();
            _LogB.write();
        }
    }
 
 
 
 
    public class LoggerTestA
    {
        ClsLogger testClsLogger = new ClsLogger("A"string.Format(Form1.__LOG_Format, "A"));
 
        public LoggerTestA()
        {
            testClsLogger.Write("startA");
        }
 
        public void write()
        {
            testClsLogger.Write($"testA_{new Random().Next()}");
        }
    }
 
    public class LoggerTestB
    {
        ClsLogger testClsLogger = new ClsLogger("CC"string.Format(Form1.__LOG_Format, "B"));
        public LoggerTestB()
        {
            testClsLogger.Write("startB");
        }
 
        public void write()
        {
            testClsLogger.Write($"testB_{new Random().Next()}");
        }
    }
 
    public class ClsLogger
    {
        private readonly NLog.Logger _Logger;
 
        public ClsLogger(string _logName, string _fileFormat)
        {
            var logFactory = new NLog.LogFactory(this.SetRule(_logName, _fileFormat));
            this._Logger = logFactory.GetLogger(_logName);
 
        }
 
        public void Write(string _value)
        {
 
            this._Logger.Trace(_value);
        }
 
        private LoggingConfiguration SetRule(string _logName, string _fileFormat)
        {
            LoggingConfiguration loggingConfiguration = new LoggingConfiguration();
            FileTarget fileTarget = new FileTarget();
            fileTarget.Name = _logName;
            fileTarget.FileName = (Layout)_fileFormat;
            fileTarget.Layout = (Layout)"[ ${date:format=yyyy-MM-dd HH\\:mm\\:ss} ] ${message}";
            loggingConfiguration.AddTarget(fileTarget.Name, (Target)fileTarget);
            loggingConfiguration.AddRule(LogLevel.Trace, LogLevel.Trace, (Target)fileTarget);
            return loggingConfiguration;
        }
    }
}
 
cs

댓글