1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Services
.Logging
.NLogIntegration
18 using Castle
.Core
.Logging
;
21 public class NLogLogger
: ILogger
23 private Logger logger
;
24 private NLogFactory factory
;
30 public NLogLogger(Logger logger
, NLogFactory factory
)
36 public virtual ILogger
CreateChildLogger(String name
)
38 return Factory
.Create(Logger
.Name
+ "." + name
);
41 protected internal Logger Logger
43 get { return logger; }
44 set { logger = value; }
47 protected internal NLogFactory Factory
49 get { return factory; }
50 set { factory = value; }
53 public override string ToString()
55 return Logger
.ToString();
61 /// Logs a debug message.
63 /// <param name="message">The message to log</param>
64 public void Debug(string message
)
66 if (Logger
.IsDebugEnabled
)
67 Logger
.Debug(message
);
71 /// Logs a debug message.
73 /// <param name="exception">The exception to log</param>
74 /// <param name="message">The message to log</param>
75 public void Debug(string message
, Exception exception
)
77 if (Logger
.IsDebugEnabled
)
78 Logger
.DebugException(message
, exception
);
82 /// Logs a debug message.
84 /// <param name="format">Format string for the message to log</param>
85 /// <param name="args">Format arguments for the message to log</param>
86 [Obsolete("Use DebugFormat instead")]
87 public void Debug(string format
, params object[] args
)
89 if (Logger
.IsDebugEnabled
)
90 Logger
.Debug(format
, args
);
94 /// Logs a debug message.
96 /// <param name="format">Format string for the message to log</param>
97 /// <param name="args">Format arguments for the message to log</param>
98 public void DebugFormat(string format
, params object[] args
)
100 if (Logger
.IsDebugEnabled
)
101 Logger
.Debug(format
, args
);
105 /// Logs a debug message.
107 /// <param name="exception">The exception to log</param>
108 /// <param name="format">Format string for the message to log</param>
109 /// <param name="args">Format arguments for the message to log</param>
110 public void DebugFormat(Exception exception
, string format
, params object[] args
)
112 if (Logger
.IsDebugEnabled
)
113 Logger
.DebugException(String
.Format(format
, args
), exception
);
117 /// Logs a debug message.
119 /// <param name="formatProvider">The format provider to use</param>
120 /// <param name="format">Format string for the message to log</param>
121 /// <param name="args">Format arguments for the message to log</param>
122 public void DebugFormat(IFormatProvider formatProvider
, string format
, params object[] args
)
124 if (Logger
.IsDebugEnabled
)
125 Logger
.Debug(formatProvider
, format
, args
);
129 /// Logs a debug message.
131 /// <param name="exception">The exception to log</param>
132 /// <param name="formatProvider">The format provider to use</param>
133 /// <param name="format">Format string for the message to log</param>
134 /// <param name="args">Format arguments for the message to log</param>
135 public void DebugFormat(Exception exception
, IFormatProvider formatProvider
, string format
, params object[] args
)
137 if (Logger
.IsDebugEnabled
)
138 Logger
.DebugException(String
.Format(formatProvider
, format
, args
), exception
);
146 /// Logs an info message.
148 /// <param name="message">The message to log</param>
149 public void Info(string message
)
151 if (Logger
.IsInfoEnabled
)
152 Logger
.Info(message
);
156 /// Logs an info message.
158 /// <param name="exception">The exception to log</param>
159 /// <param name="message">The message to log</param>
160 public void Info(string message
, Exception exception
)
162 if (Logger
.IsInfoEnabled
)
163 Logger
.InfoException(message
, exception
);
167 /// Logs an info message.
169 /// <param name="format">Format string for the message to log</param>
170 /// <param name="args">Format arguments for the message to log</param>
171 [Obsolete("Use InfoFormat instead")]
172 public void Info(string format
, params object[] args
)
174 if (Logger
.IsInfoEnabled
)
175 Logger
.Info(format
, args
);
179 /// Logs an info message.
181 /// <param name="format">Format string for the message to log</param>
182 /// <param name="args">Format arguments for the message to log</param>
183 public void InfoFormat(string format
, params object[] args
)
185 if (Logger
.IsInfoEnabled
)
186 Logger
.Info(format
, args
);
190 /// Logs an info message.
192 /// <param name="exception">The exception to log</param>
193 /// <param name="format">Format string for the message to log</param>
194 /// <param name="args">Format arguments for the message to log</param>
195 public void InfoFormat(Exception exception
, string format
, params object[] args
)
197 if (Logger
.IsInfoEnabled
)
198 Logger
.InfoException(String
.Format(format
, args
), exception
);
202 /// Logs an info message.
204 /// <param name="formatProvider">The format provider to use</param>
205 /// <param name="format">Format string for the message to log</param>
206 /// <param name="args">Format arguments for the message to log</param>
207 public void InfoFormat(IFormatProvider formatProvider
, string format
, params object[] args
)
209 if (Logger
.IsInfoEnabled
)
210 Logger
.Info(formatProvider
, format
, args
);
214 /// Logs an info message.
216 /// <param name="exception">The exception to log</param>
217 /// <param name="formatProvider">The format provider to use</param>
218 /// <param name="format">Format string for the message to log</param>
219 /// <param name="args">Format arguments for the message to log</param>
220 public void InfoFormat(Exception exception
, IFormatProvider formatProvider
, string format
, params object[] args
)
222 if (Logger
.IsInfoEnabled
)
223 Logger
.InfoException(String
.Format(formatProvider
, format
, args
), exception
);
231 /// Logs a warn message.
233 /// <param name="message">The message to log</param>
234 public void Warn(string message
)
236 if (Logger
.IsWarnEnabled
)
237 Logger
.Warn(message
);
241 /// Logs a warn message.
243 /// <param name="exception">The exception to log</param>
244 /// <param name="message">The message to log</param>
245 public void Warn(string message
, Exception exception
)
247 if (Logger
.IsWarnEnabled
)
248 Logger
.WarnException(message
, exception
);
252 /// Logs a warn message.
254 /// <param name="format">Format string for the message to log</param>
255 /// <param name="args">Format arguments for the message to log</param>
256 [Obsolete("Use WarnFormat instead")]
257 public void Warn(string format
, params object[] args
)
259 if (Logger
.IsWarnEnabled
)
260 Logger
.Warn(format
, args
);
264 /// Logs a warn message.
266 /// <param name="format">Format string for the message to log</param>
267 /// <param name="args">Format arguments for the message to log</param>
268 public void WarnFormat(string format
, params object[] args
)
270 if (Logger
.IsWarnEnabled
)
271 Logger
.Warn(format
, args
);
275 /// Logs a warn message.
277 /// <param name="exception">The exception to log</param>
278 /// <param name="format">Format string for the message to log</param>
279 /// <param name="args">Format arguments for the message to log</param>
280 public void WarnFormat(Exception exception
, string format
, params object[] args
)
282 if (Logger
.IsWarnEnabled
)
283 Logger
.WarnException(String
.Format(format
, args
), exception
);
287 /// Logs a warn message.
289 /// <param name="formatProvider">The format provider to use</param>
290 /// <param name="format">Format string for the message to log</param>
291 /// <param name="args">Format arguments for the message to log</param>
292 public void WarnFormat(IFormatProvider formatProvider
, string format
, params object[] args
)
294 if (Logger
.IsWarnEnabled
)
295 Logger
.Warn(formatProvider
, format
, args
);
299 /// Logs a warn message.
301 /// <param name="exception">The exception to log</param>
302 /// <param name="formatProvider">The format provider to use</param>
303 /// <param name="format">Format string for the message to log</param>
304 /// <param name="args">Format arguments for the message to log</param>
305 public void WarnFormat(Exception exception
, IFormatProvider formatProvider
, string format
, params object[] args
)
307 if (Logger
.IsWarnEnabled
)
308 Logger
.WarnException(String
.Format(formatProvider
, format
, args
), exception
);
316 /// Logs an error message.
318 /// <param name="message">The message to log</param>
319 public void Error(string message
)
321 if (Logger
.IsErrorEnabled
)
322 Logger
.Error(message
);
326 /// Logs an error message.
328 /// <param name="exception">The exception to log</param>
329 /// <param name="message">The message to log</param>
330 public void Error(string message
, Exception exception
)
332 if (Logger
.IsErrorEnabled
)
333 Logger
.ErrorException(message
, exception
);
337 /// Logs an error message.
339 /// <param name="format">Format string for the message to log</param>
340 /// <param name="args">Format arguments for the message to log</param>
341 [Obsolete("Use ErrorFormat instead")]
342 public void Error(string format
, params object[] args
)
344 if (Logger
.IsErrorEnabled
)
345 Logger
.Error(format
, args
);
349 /// Logs an error message.
351 /// <param name="format">Format string for the message to log</param>
352 /// <param name="args">Format arguments for the message to log</param>
353 public void ErrorFormat(string format
, params object[] args
)
355 if (Logger
.IsErrorEnabled
)
356 Logger
.Error(format
, args
);
360 /// Logs an error message.
362 /// <param name="exception">The exception to log</param>
363 /// <param name="format">Format string for the message to log</param>
364 /// <param name="args">Format arguments for the message to log</param>
365 public void ErrorFormat(Exception exception
, string format
, params object[] args
)
367 if (Logger
.IsErrorEnabled
)
368 Logger
.ErrorException(String
.Format(format
, args
), exception
);
372 /// Logs an error message.
374 /// <param name="formatProvider">The format provider to use</param>
375 /// <param name="format">Format string for the message to log</param>
376 /// <param name="args">Format arguments for the message to log</param>
377 public void ErrorFormat(IFormatProvider formatProvider
, string format
, params object[] args
)
379 if (Logger
.IsErrorEnabled
)
380 Logger
.Error(formatProvider
, format
, args
);
384 /// Logs an error message.
386 /// <param name="exception">The exception to log</param>
387 /// <param name="formatProvider">The format provider to use</param>
388 /// <param name="format">Format string for the message to log</param>
389 /// <param name="args">Format arguments for the message to log</param>
390 public void ErrorFormat(Exception exception
, IFormatProvider formatProvider
, string format
, params object[] args
)
392 if (Logger
.IsErrorEnabled
)
393 Logger
.ErrorException(String
.Format(formatProvider
, format
, args
), exception
);
401 /// Logs a fatal message.
403 /// <param name="message">The message to log</param>
404 public void Fatal(string message
)
406 if (Logger
.IsFatalEnabled
)
407 Logger
.Fatal(message
);
411 /// Logs a fatal message.
413 /// <param name="exception">The exception to log</param>
414 /// <param name="message">The message to log</param>
415 public void Fatal(string message
, Exception exception
)
417 if (Logger
.IsFatalEnabled
)
418 Logger
.FatalException(message
, exception
);
422 /// Logs a fatal message.
424 /// <param name="format">Format string for the message to log</param>
425 /// <param name="args">Format arguments for the message to log</param>
426 [Obsolete("Use FatalFormat instead")]
427 public void Fatal(string format
, params object[] args
)
429 if (Logger
.IsFatalEnabled
)
430 Logger
.Fatal(format
, args
);
434 /// Logs a fatal message.
436 /// <param name="format">Format string for the message to log</param>
437 /// <param name="args">Format arguments for the message to log</param>
438 public void FatalFormat(string format
, params object[] args
)
440 if (Logger
.IsFatalEnabled
)
441 Logger
.Fatal(format
, args
);
445 /// Logs a fatal message.
447 /// <param name="exception">The exception to log</param>
448 /// <param name="format">Format string for the message to log</param>
449 /// <param name="args">Format arguments for the message to log</param>
450 public void FatalFormat(Exception exception
, string format
, params object[] args
)
452 if (Logger
.IsFatalEnabled
)
453 Logger
.FatalException(String
.Format(format
, args
), exception
);
457 /// Logs a fatal message.
459 /// <param name="formatProvider">The format provider to use</param>
460 /// <param name="format">Format string for the message to log</param>
461 /// <param name="args">Format arguments for the message to log</param>
462 public void FatalFormat(IFormatProvider formatProvider
, string format
, params object[] args
)
464 if (Logger
.IsFatalEnabled
)
465 Logger
.Fatal(formatProvider
, format
, args
);
469 /// Logs a fatal message.
471 /// <param name="exception">The exception to log</param>
472 /// <param name="formatProvider">The format provider to use</param>
473 /// <param name="format">Format string for the message to log</param>
474 /// <param name="args">Format arguments for the message to log</param>
475 public void FatalFormat(Exception exception
, IFormatProvider formatProvider
, string format
, params object[] args
)
477 if (Logger
.IsFatalEnabled
)
478 Logger
.FatalException(String
.Format(formatProvider
, format
, args
), exception
);
483 #region FatalError (obsolete)
485 [Obsolete("Use FatalFormat instead")]
486 public void FatalError(String format
, params object[] args
)
488 if (Logger
.IsFatalEnabled
)
489 Logger
.Fatal(format
, args
);
492 [Obsolete("Use Fatal instead")]
493 public void FatalError(String message
, Exception exception
)
495 if (Logger
.IsFatalEnabled
)
496 Logger
.FatalException(message
, exception
);
499 [Obsolete("Use Fatal instead")]
500 public void FatalError(String message
)
502 if (Logger
.IsFatalEnabled
)
503 Logger
.Fatal(message
);
508 #region Is (...) Enabled
511 /// Determines if messages of priority "debug" will be logged.
513 /// <value>True if "debug" messages will be logged.</value>
514 public bool IsDebugEnabled
516 get { return Logger.IsDebugEnabled; }
520 /// Determines if messages of priority "info" will be logged.
522 /// <value>True if "info" messages will be logged.</value>
523 public bool IsInfoEnabled
525 get { return Logger.IsInfoEnabled; }
529 /// Determines if messages of priority "warn" will be logged.
531 /// <value>True if "warn" messages will be logged.</value>
532 public bool IsWarnEnabled
534 get { return Logger.IsWarnEnabled; }
538 /// Determines if messages of priority "error" will be logged.
540 /// <value>True if "error" messages will be logged.</value>
541 public bool IsErrorEnabled
543 get { return Logger.IsErrorEnabled; }
547 /// Determines if messages of priority "fatal" will be logged.
549 /// <value>True if "fatal" messages will be logged.</value>
550 public bool IsFatalEnabled
552 get { return Logger.IsFatalEnabled; }
555 [Obsolete("Use IsFatalEnabled instead")]
556 public bool IsFatalErrorEnabled
558 get { return Logger.IsFatalEnabled; }