Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Tracing/3.3/Logger.cs @ 9793

Last change on this file since 9793 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Diagnostics;
24using System.IO;
25using HeuristicLab.Tracing.Properties;
26using log4net;
27using log4net.Config;
28
29namespace HeuristicLab.Tracing {
30
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>
35  public class Logger {
36
37    /// <summary>
38    /// true if Configure has been called already.
39    /// </summary>
40    protected static bool IsConfigured = false;
41
42    /// <summary>
43    /// Configures this instance: Reads the log file specified in the settings.
44    /// </summary>
45    protected static void Configure() {
46      if (IsConfigured) return;
47      IsConfigured = true;
48      if (string.IsNullOrEmpty(Settings.Default.TracingLog4netConfigFile)) {
49        Settings.Default.TracingLog4netConfigFile =
50            "HeuristicLab.log4net.xml";
51      }
52      XmlConfigurator.ConfigureAndWatch(
53        new FileInfo(Settings.Default.TracingLog4netConfigFile));
54      Info("logging initialized " + DateTime.Now);
55    }
56
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>
63    public static ILog GetDefaultLogger(int nParents) {
64      Configure();
65      StackFrame frame = new StackFrame(nParents + 1);
66      return LogManager.GetLogger(frame.GetMethod().DeclaringType);
67    }
68
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>
74    public static ILog GetDefaultLogger() {
75      Configure();
76      StackFrame frame = new StackFrame(1);
77      return LogManager.GetLogger(frame.GetMethod().DeclaringType);
78    }
79
80    /// <summary>
81    /// Issues a debug message to the default logger.
82    /// </summary>
83    /// <param name="message">The message.</param>
84    public static void Debug(object message) {
85      GetDefaultLogger(1).Debug(message);
86    }
87
88    /// <summary>
89    /// Issues an informational message to the default logger.
90    /// </summary>
91    /// <param name="message">The message.</param>
92    public static void Info(object message) {
93      GetDefaultLogger(1).Info(message);
94    }
95
96    /// <summary>
97    /// Issues a warning message to the default logger.
98    /// </summary>
99    /// <param name="message">The message.</param>
100    public static void Warn(object message) {
101      GetDefaultLogger(1).Warn(message);
102    }
103
104    /// <summary>
105    /// Issues an error message to the default logger.
106    /// </summary>
107    /// <param name="message">The message.</param>
108    public static void Error(object message) {
109      GetDefaultLogger(1).Error(message);
110    }
111
112    /// <summary>
113    /// Issues a fatal error message to the default logger.
114    /// </summary>
115    /// <param name="message">The message.</param>
116    public static void Fatal(object message) {
117      GetDefaultLogger(1).Fatal(message);
118    }
119
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>
125    public static void Debug(Type type, object message) {
126      Configure();
127      LogManager.GetLogger(type).Debug(message);
128    }
129
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>
136    public static void Info(Type type, object message) {
137      Configure();
138      LogManager.GetLogger(type).Info(message);
139    }
140
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>
147    public static void Warn(Type type, object message) {
148      Configure();
149      LogManager.GetLogger(type).Warn(message);
150    }
151
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>
158    public static void Error(Type type, object message) {
159      Configure();
160      LogManager.GetLogger(type).Error(message);
161    }
162
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>
169    public static void Fatal(Type type, object message) {
170      Configure();
171      LogManager.GetLogger(type).Fatal(message);
172    }
173
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>
180    public static void Debug(object message, Exception exception) {
181      GetDefaultLogger(1).Debug(message, exception);
182    }
183
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
191    public static void Info(object message, Exception exception) {
192      GetDefaultLogger(1).Info(message, exception);
193    }
194
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>
201    public static void Warn(object message, Exception exception) {
202      GetDefaultLogger(1).Warn(message, exception);
203    }
204
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>
211    public static void Error(object message, Exception exception) {
212      GetDefaultLogger(1).Error(message, exception);
213    }
214
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>
221    public static void Fatal(object message, Exception exception) {
222      GetDefaultLogger(1).Fatal(message, exception);
223    }
224
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>
232    public static void Debug(Type type, object message, Exception exception) {
233      Configure();
234      LogManager.GetLogger(type).Debug(message, exception);
235    }
236
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>
244    public static void Info(Type type, object message, Exception exception) {
245      Configure();
246      LogManager.GetLogger(type).Info(message, exception);
247    }
248
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>
256    public static void Warn(Type type, object message, Exception exception) {
257      Configure();
258      LogManager.GetLogger(type).Warn(message, exception);
259    }
260
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>
268    public static void Error(Type type, object message, Exception exception) {
269      Configure();
270      LogManager.GetLogger(type).Error(message, exception);
271    }
272
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>
280    public static void Fatal(Type type, object message, Exception exception) {
281      Configure();
282      LogManager.GetLogger(type).Fatal(message, exception);
283    }
284  }
285}
Note: See TracBrowser for help on using the repository browser.