Fixed Service-10. ExtendedLog4netLogger and ExtendedNLogLogger don't handle CreateCh...
[castle.git] / Services / Logging / Castle.Services.Logging.NLogIntegration / NLogLogger.cs
blobe6801f570894519fbed62960325f0e316548c6cc
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using Castle.Core.Logging;
19 using NLog;
21 public class NLogLogger : ILogger
23 private Logger logger;
24 private NLogFactory factory;
26 internal NLogLogger()
30 public NLogLogger(Logger logger, NLogFactory factory)
32 Logger = logger;
33 Factory = 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();
58 #region Debug
60 /// <summary>
61 /// Logs a debug message.
62 /// </summary>
63 /// <param name="message">The message to log</param>
64 public void Debug(string message)
66 if (Logger.IsDebugEnabled)
67 Logger.Debug(message);
70 /// <summary>
71 /// Logs a debug message.
72 /// </summary>
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);
81 /// <summary>
82 /// Logs a debug message.
83 /// </summary>
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);
93 /// <summary>
94 /// Logs a debug message.
95 /// </summary>
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);
104 /// <summary>
105 /// Logs a debug message.
106 /// </summary>
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);
116 /// <summary>
117 /// Logs a debug message.
118 /// </summary>
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);
128 /// <summary>
129 /// Logs a debug message.
130 /// </summary>
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);
141 #endregion
143 #region Info
145 /// <summary>
146 /// Logs an info message.
147 /// </summary>
148 /// <param name="message">The message to log</param>
149 public void Info(string message)
151 if (Logger.IsInfoEnabled)
152 Logger.Info(message);
155 /// <summary>
156 /// Logs an info message.
157 /// </summary>
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);
166 /// <summary>
167 /// Logs an info message.
168 /// </summary>
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);
178 /// <summary>
179 /// Logs an info message.
180 /// </summary>
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);
189 /// <summary>
190 /// Logs an info message.
191 /// </summary>
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);
201 /// <summary>
202 /// Logs an info message.
203 /// </summary>
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);
213 /// <summary>
214 /// Logs an info message.
215 /// </summary>
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);
226 #endregion
228 #region Warn
230 /// <summary>
231 /// Logs a warn message.
232 /// </summary>
233 /// <param name="message">The message to log</param>
234 public void Warn(string message)
236 if (Logger.IsWarnEnabled)
237 Logger.Warn(message);
240 /// <summary>
241 /// Logs a warn message.
242 /// </summary>
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);
251 /// <summary>
252 /// Logs a warn message.
253 /// </summary>
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);
263 /// <summary>
264 /// Logs a warn message.
265 /// </summary>
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);
274 /// <summary>
275 /// Logs a warn message.
276 /// </summary>
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);
286 /// <summary>
287 /// Logs a warn message.
288 /// </summary>
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);
298 /// <summary>
299 /// Logs a warn message.
300 /// </summary>
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);
311 #endregion
313 #region Error
315 /// <summary>
316 /// Logs an error message.
317 /// </summary>
318 /// <param name="message">The message to log</param>
319 public void Error(string message)
321 if (Logger.IsErrorEnabled)
322 Logger.Error(message);
325 /// <summary>
326 /// Logs an error message.
327 /// </summary>
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);
336 /// <summary>
337 /// Logs an error message.
338 /// </summary>
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);
348 /// <summary>
349 /// Logs an error message.
350 /// </summary>
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);
359 /// <summary>
360 /// Logs an error message.
361 /// </summary>
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);
371 /// <summary>
372 /// Logs an error message.
373 /// </summary>
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);
383 /// <summary>
384 /// Logs an error message.
385 /// </summary>
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);
396 #endregion
398 #region Fatal
400 /// <summary>
401 /// Logs a fatal message.
402 /// </summary>
403 /// <param name="message">The message to log</param>
404 public void Fatal(string message)
406 if (Logger.IsFatalEnabled)
407 Logger.Fatal(message);
410 /// <summary>
411 /// Logs a fatal message.
412 /// </summary>
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);
421 /// <summary>
422 /// Logs a fatal message.
423 /// </summary>
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);
433 /// <summary>
434 /// Logs a fatal message.
435 /// </summary>
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);
444 /// <summary>
445 /// Logs a fatal message.
446 /// </summary>
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);
456 /// <summary>
457 /// Logs a fatal message.
458 /// </summary>
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);
468 /// <summary>
469 /// Logs a fatal message.
470 /// </summary>
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);
481 #endregion
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);
506 #endregion
508 #region Is (...) Enabled
510 /// <summary>
511 /// Determines if messages of priority "debug" will be logged.
512 /// </summary>
513 /// <value>True if "debug" messages will be logged.</value>
514 public bool IsDebugEnabled
516 get { return Logger.IsDebugEnabled; }
519 /// <summary>
520 /// Determines if messages of priority "info" will be logged.
521 /// </summary>
522 /// <value>True if "info" messages will be logged.</value>
523 public bool IsInfoEnabled
525 get { return Logger.IsInfoEnabled; }
528 /// <summary>
529 /// Determines if messages of priority "warn" will be logged.
530 /// </summary>
531 /// <value>True if "warn" messages will be logged.</value>
532 public bool IsWarnEnabled
534 get { return Logger.IsWarnEnabled; }
537 /// <summary>
538 /// Determines if messages of priority "error" will be logged.
539 /// </summary>
540 /// <value>True if "error" messages will be logged.</value>
541 public bool IsErrorEnabled
543 get { return Logger.IsErrorEnabled; }
546 /// <summary>
547 /// Determines if messages of priority "fatal" will be logged.
548 /// </summary>
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; }
561 #endregion