Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tracing/3.3/Logger.cs @ 4311

Last change on this file since 4311 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 9.8 KB
RevLine 
[2645]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2645]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4068]23using System.Diagnostics;
24using System.IO;
[1467]25using HeuristicLab.Tracing.Properties;
26using log4net;
27using log4net.Config;
28
29namespace HeuristicLab.Tracing {
30
[3057]31  /// <summary>
32  /// HeuristicLab Tracing entry point. Default logger. Reads configured tracing
33  /// file and provides automatic logging with reflection of the calling type.
34  /// </summary>
[1467]35  public class Logger {
36
[3057]37    /// <summary>
38    /// true if Configure has been called already.
39    /// </summary>
[1931]40    protected static bool IsConfigured = false;
[1467]41
[3057]42    /// <summary>
43    /// Configures this instance: Reads the log file specified in the settings.
44    /// </summary>
[1933]45    protected static void Configure() {
[2591]46      if (IsConfigured) return;
[1467]47      IsConfigured = true;
[1622]48      if (string.IsNullOrEmpty(Settings.Default.TracingLog4netConfigFile)) {
[2591]49        Settings.Default.TracingLog4netConfigFile =
50            "HeuristicLab.log4net.xml";
[1467]51      }
52      XmlConfigurator.ConfigureAndWatch(
[1622]53        new FileInfo(Settings.Default.TracingLog4netConfigFile));
[1473]54      Info("logging initialized " + DateTime.Now);
[1467]55    }
56
[3057]57    /// <summary>
58    /// Gets the default logger for the calling class n levels up in the
59    /// call hierarchy.
60    /// </summary>
61    /// <param name="nParents">The number of parent calls.</param>
62    /// <returns>An <see cref="ILog"/> instance.</returns>
[1467]63    public static ILog GetDefaultLogger(int nParents) {
64      Configure();
65      StackFrame frame = new StackFrame(nParents + 1);
[2591]66      return LogManager.GetLogger(frame.GetMethod().DeclaringType);
[1467]67    }
68
[3057]69    /// <summary>
70    /// Gets the default logger: The logger for the class of the
71    /// calling method.
72    /// </summary>
73    /// <returns>An <see cref="ILog"/> instance.</returns>
[1473]74    public static ILog GetDefaultLogger() {
75      Configure();
76      StackFrame frame = new StackFrame(1);
77      return LogManager.GetLogger(frame.GetMethod().DeclaringType);
78    }
79
[3057]80    /// <summary>
81    /// Issues a debug message to the default logger.
82    /// </summary>
83    /// <param name="message">The message.</param>
[1467]84    public static void Debug(object message) {
85      GetDefaultLogger(1).Debug(message);
86    }
87
[3057]88    /// <summary>
89    /// Issues an informational message to the default logger.
90    /// </summary>
91    /// <param name="message">The message.</param>
[1467]92    public static void Info(object message) {
93      GetDefaultLogger(1).Info(message);
94    }
95
[3057]96    /// <summary>
97    /// Issues a warning message to the default logger.
98    /// </summary>
99    /// <param name="message">The message.</param>
[1467]100    public static void Warn(object message) {
101      GetDefaultLogger(1).Warn(message);
102    }
103
[3057]104    /// <summary>
105    /// Issues an error message to the default logger.
106    /// </summary>
107    /// <param name="message">The message.</param>
[2591]108    public static void Error(object message) {
[1467]109      GetDefaultLogger(1).Error(message);
110    }
111
[3057]112    /// <summary>
113    /// Issues a fatal error message to the default logger.
114    /// </summary>
115    /// <param name="message">The message.</param>
[1467]116    public static void Fatal(object message) {
117      GetDefaultLogger(1).Fatal(message);
118    }
119
[3057]120    /// <summary>
121    /// Issues a debug message to the logger of the specified type.
122    /// </summary>
123    /// <param name="type">The type.</param>
124    /// <param name="message">The message.</param>
[1467]125    public static void Debug(Type type, object message) {
126      Configure();
[1473]127      LogManager.GetLogger(type).Debug(message);
[1467]128    }
129
[3057]130    /// <summary>
131    /// Issues an iformational message to the logger of the specified
132    /// type.
133    /// </summary>
134    /// <param name="type">The type.</param>
135    /// <param name="message">The message.</param>
[1467]136    public static void Info(Type type, object message) {
137      Configure();
[2591]138      LogManager.GetLogger(type).Info(message);
[1467]139    }
140
[3057]141    /// <summary>
142    /// Issues a warning message to the logger of
143    /// the specified type.
144    /// </summary>
145    /// <param name="type">The type.</param>
146    /// <param name="message">The message.</param>
[1467]147    public static void Warn(Type type, object message) {
148      Configure();
[2591]149      LogManager.GetLogger(type).Warn(message);
[1467]150    }
151
[3057]152    /// <summary>
153    /// Issues an error message to the logger of the specified
154    /// type.
155    /// </summary>
156    /// <param name="type">The type.</param>
157    /// <param name="message">The message.</param>
[1467]158    public static void Error(Type type, object message) {
159      Configure();
160      LogManager.GetLogger(type).Error(message);
161    }
162
[3057]163    /// <summary>
164    /// Issues a fatal error message to the logger of
165    /// the specified type.
166    /// </summary>
167    /// <param name="type">The type.</param>
168    /// <param name="message">The message.</param>
[1467]169    public static void Fatal(Type type, object message) {
170      Configure();
171      LogManager.GetLogger(type).Fatal(message);
172    }
173
[3057]174    /// <summary>
175    /// Issues a debug message to the default
176    /// logger including an exception.
177    /// </summary>
178    /// <param name="message">The message.</param>
179    /// <param name="exception">The exception.</param>
[1473]180    public static void Debug(object message, Exception exception) {
181      GetDefaultLogger(1).Debug(message, exception);
182    }
183
[3057]184    /// <summary>
185    /// Issues an informational message to the default
186    /// logger including an exception.
187    /// </summary>
188    /// <param name="message">The message.</param>
189    /// <param name="exception">The exception.</param>
190
[1473]191    public static void Info(object message, Exception exception) {
192      GetDefaultLogger(1).Info(message, exception);
193    }
194
[3057]195    /// <summary>
196    /// Issues a warning message to the default
197    /// logger including an exception.
198    /// </summary>
199    /// <param name="message">The message.</param>
200    /// <param name="exception">The exception.</param>
[1473]201    public static void Warn(object message, Exception exception) {
202      GetDefaultLogger(1).Warn(message, exception);
203    }
204
[3057]205    /// <summary>
206    /// Issues an error message to the default
207    /// logger including an exception.
208    /// </summary>
209    /// <param name="message">The message.</param>
210    /// <param name="exception">The exception.</param>
[1473]211    public static void Error(object message, Exception exception) {
212      GetDefaultLogger(1).Error(message, exception);
213    }
214
[3057]215    /// <summary>
216    /// Issues a fatal error message to the default
217    /// logger including an exception.
218    /// </summary>
219    /// <param name="message">The message.</param>
220    /// <param name="exception">The exception.</param>
[1473]221    public static void Fatal(object message, Exception exception) {
222      GetDefaultLogger(1).Fatal(message, exception);
223    }
224
[3057]225    /// <summary>
226    /// Issues a debug message to the logger of the specified
227    /// type including an exception.
228    /// </summary>
229    /// <param name="type">The type.</param>
230    /// <param name="message">The message.</param>
231    /// <param name="exception">The exception.</param>
[1473]232    public static void Debug(Type type, object message, Exception exception) {
233      Configure();
234      LogManager.GetLogger(type).Debug(message, exception);
235    }
236
[3057]237    /// <summary>
238    /// Issues an informational message to the logger of the specified
239    /// type including an exception.
240    /// </summary>
241    /// <param name="type">The type.</param>
242    /// <param name="message">The message.</param>
243    /// <param name="exception">The exception.</param>
[1473]244    public static void Info(Type type, object message, Exception exception) {
245      Configure();
246      LogManager.GetLogger(type).Info(message, exception);
247    }
248
[3057]249    /// <summary>
250    /// Issues a warning message to the logger of the specified
251    /// type including an exception.
252    /// </summary>
253    /// <param name="type">The type.</param>
254    /// <param name="message">The message.</param>
255    /// <param name="exception">The exception.</param>
[1473]256    public static void Warn(Type type, object message, Exception exception) {
257      Configure();
258      LogManager.GetLogger(type).Warn(message, exception);
259    }
260
[3057]261    /// <summary>
262    /// Issues an error message to the logger of the specified
263    /// type including an exception.
264    /// </summary>
265    /// <param name="type">The type.</param>
266    /// <param name="message">The message.</param>
267    /// <param name="exception">The exception.</param>
[1473]268    public static void Error(Type type, object message, Exception exception) {
269      Configure();
270      LogManager.GetLogger(type).Error(message, exception);
271    }
272
[3057]273    /// <summary>
274    /// Issues a fatal error message to the logger of the specified
275    /// type including an exception.
276    /// </summary>
277    /// <param name="type">The type.</param>
278    /// <param name="message">The message.</param>
279    /// <param name="exception">The exception.</param>
[1473]280    public static void Fatal(Type type, object message, Exception exception) {
281      Configure();
282      LogManager.GetLogger(type).Fatal(message, exception);
283    }
[1467]284  }
285}
Note: See TracBrowser for help on using the repository browser.