■ LogEventInfo 클래스의 Properties 속성을 사용해 로그 데이터를 전달하는 방법을 보여준다.
▶ 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 31 32 33 34 35 36 |
<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> |
▶ 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 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
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 } } |