첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ NLog.config

<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}}|${message}${newline}
${event-properties:filePath}|L:${event-properties:lineNumber}|${event-properties:memberName}
${onexception:${newline}${exception:format=tostring}}"
            fileName=".\Log\TestProject.log"
            archiveFileName=".\Log\TestProject_{#}.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}}|${message}${newline}
${event-properties:filePath}|L:${event-properties:lineNumber}|${event-properties:memberName}
${onexception:${newline}${exception:format=tostring}}" />
        <target name="eventlog"
            xsi:Type="EventLog"
            layout="${date:format=HH\:mm\:ss.fff}|${uppercase:${level}}|${message}${newline}
${event-properties:filePath}|L:${event-properties:lineNumber}|${event-properties:memberName}
${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>

 

728x90

 

▶ Program.cs

using System;
using System.Runtime.CompilerServices;

using NLog;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 로거 Logger

        /// <summary>
        /// 로거
        /// </summary>
        private static Logger _logger { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 로거 설정하기- SetLogger()

        /// <summary>
        /// 로거 설정하기
        /// </summary>
        private static void SetLogger()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }

        #endregion
        #region 추적 로그 작성하기 - LogTrace(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 추적 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogTrace
        (
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Trace, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 디버그 로그 작성하기 - LogDebug(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 디버그 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogDebug
        (
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Debug, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 정보 로그 작성하기 - LogInfo(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 정보 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogInfo
        (
            string                    message,
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0,
            [CallerMemberName] string memberName = ""
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Info, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 경고 로그 작성하기 - LogWarn(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 경고 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogWarn
        (
            string                    message,
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0,
            [CallerMemberName] string memberName = ""
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Warn, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 에러 로그 작성하기 - LogError(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 에러 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogError
        (
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Error, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 에러 로그 작성하기 - LogError(exception, message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 에러 로그 작성하기
        /// </summary>
        /// <param name="exception">예외</param>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogError
        (
            Exception                 exception,
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Error, null, message)
            {
                Exception = exception
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 치명적 에러 로그 작성하기 - LogFatal(message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 치명적 에러 로그 작성하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogFatal
        (
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Fatal, null, message)
            {
                Exception = null
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion
        #region 치명적 에러 로그 작성하기 - LogFatal(exception, message, sourceFilePath, sourceLineNumber, memberName)

        /// <summary>
        /// 치명적 에러 로그 작성하기
        /// </summary>
        /// <param name="exception">예외</param>
        /// <param name="message">메시지</param>
        /// <param name="filePath">소스 파일 경로</param>
        /// <param name="lineNumber">소스 라인 번호</param>
        /// <param name="memberName">멤버명</param>
        private static void LogFatal
        (
            Exception                 exception,
            string                    message,
            [CallerMemberName] string memberName = "",
            [CallerFilePath] string   sourceFilePath = "",
            [CallerLineNumber] int    sourceLineNumber = 0
        )
        {
            LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Fatal, null, message)
            {
                Exception = exception
            };

            logEventInfo.Properties["filePath"  ] = sourceFilePath;
            logEventInfo.Properties["lineNumber"] = sourceLineNumber;
            logEventInfo.Properties["memberName"] = memberName;

            _logger.Log(logEventInfo);
        }

        #endregion

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            SetLogger();

            LogTrace("추적 로그");
            LogDebug("디버그 로그");
            LogInfo("정보 로그");
            LogWarn("경로 로그");
            LogError("에러 로그");
            LogError(new ArgumentNullException("test"), "에러 로그");
            LogError("치명적 에러 로그");
            LogError(new ArgumentNullException("test"), "치명적 에러 로그");
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)

'C# > NLog' 카테고리의 다른 글

[C#/NLOG] NLog.config 파일을 포함 리소스로 사용하기  (0) 2021.02.01
[C#/NLOG] NLOG 사용하기  (0) 2021.02.01
Posted by icodebroker

댓글을 달아 주세요