■ NLog.config 파일을 포함 리소스로 사용하는 방법을 보여준다.
▶ NLog.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 28 29 30 |
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="www.w3.org/2001/XMLSchema-instance"> <targets> <target name="file" xsi:type="File" layout="${date:format=HH\:mm\:ss.fff} | ${uppercase:${level:padding=-5}} | ${logger} | ${message}${onexception:${newline}${exception:format=tostring}}" fileName=".\Logs\Updater.log" archiveFileName=".\Logs\Updater_{#}.log" maxArchiveFiles="14" archiveEvery="Day" archiveNumbering="DateAndSequence" archiveAboveSize="104857600" archiveDateFormat="yyyyMMdd" /> <target name="console" xsi:Type="ColoredConsole" layout="${date:format=HH\:mm\:ss.fff} | ${uppercase:${level:padding=-5}} | ${logger} | ${message}${onexception:${newline}${exception:format=tostring}}" /> <target name="eventlog" xsi:Type="EventLog" layout="${date:format=HH\:mm\:ss.fff} | ${uppercase:${level:padding=-5}} | ${logger} | ${message}${onexception:${newline}${exception:format=tostring}}" machineName="." log="Application" source="TestService" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> <logger name="*" minlevel="Debug" wrtieTo="console" /> <logger name="*" minlevel="Debug" writeTo="eventlog" /> </rules> </nlog> |
※ 상기 파일 속성을 아래와 같이 설정한다.
빌드 작업 : 포함 리소스
출력 디렉터리에 복사 : 복사 안 함
▶ 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 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 |
using System; using System.IO; using System.Reflection; using System.Xml; using NLog; using NLog.Config; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Assembly assembly = Assembly.GetExecutingAssembly(); string resourceName = $"{typeof(Program).Namespace}.NLog.config"; using(Stream stream = assembly.GetManifestResourceStream(resourceName)) { using(XmlReader reader = XmlReader.Create(stream)) { LogManager.Configuration = new XmlLoggingConfiguration(reader, null); } } Logger logger = LogManager.GetCurrentClassLogger(); logger.Debug("DEBUG MESSAGE"); logger.Warn("WARNING MESSAGE"); logger.Error(new Exception("TEST ERROR"), "ERROR MESSAGE"); logger.Fatal("FATAL MESSAGE"); logger.Info("INFO MESSAGE"); } #endregion } } |