하고재비

[C#] C# Log4Net log남기기 본문

C# Windows Forms WPF

[C#] C# Log4Net log남기기

DeadDE 2018. 12. 9. 14:46

Lg4net


URL : https://logging.apache.org/log4net/download_log4net.cgi

에서 log4net-2.0.8-bin-newkey.zip 다운받고, 압축풀기


log4net.dll 추가



AssemblyInfo.cs 추가

1
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
cs


log4net.config 설정


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
<?xml version="1.0" encoding="utf-8" ?>
 
<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>
<!-- console 버전 -->
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
  </appender>
<!-- file 버전 -->
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file value="c:/ServiceLogs/DeadDE.log" />  <!-- log file 설치경로 -->
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>
cs


Program.cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Log4net_ConsoleApplication
{
    class Program
    {
        private static readonly log4net.ILog log = 
            log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        static void Main(string[] args)
        {
            log.Info("Hello logging world!");
            log.Debug("Deadde _ Debug");
            log.Info("deadde_log : Info");
            log.Warn("deadde_log : Warn");
            log.Error("deadde_log : Error");
            log.Fatal("deadde_log : Fatal");
 
            Console.Read();
        }
    }
}
 
cs


Lot4net log의 레벨

Info     - 동작로그 등의 정보

Debug - 디버그 메세지

Warn   - 주의

Error    - error

Fatal    - 시스템 정지



확인

consol, logfile 확인



'C# Windows Forms WPF' 카테고리의 다른 글

[C#] 프로그램 방식의 click event 발생  (0) 2019.09.03
[C#] 지수 변환  (0) 2019.06.14
[C#] 콘솔창 유지  (0) 2019.06.02
[WPF] Datagrid and MDB  (0) 2018.12.09
[WPF] User Control Example for WPF  (0) 2018.12.09
Comments