source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/UEUserGuide/Logger.cs

Last change on this file was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using log4net;
5using log4net.Config;
6
7namespace UEUserGuide
8{
9    /// <summary>
10    /// This class provide the logging/tracing functionalities.
11    /// </summary>
12    public sealed class Logger
13    {
14        ///////////////////////////////////////////////////////////////////////
15        #region Internal fields
16
17        /// <summary>
18        /// Logger component reference (log4net).
19        /// </summary>
20        private ILog _logger;
21
22        #endregion Internal fields
23
24        ///////////////////////////////////////////////////////////////////////
25        #region Thread-safe Singleton implementation
26
27        /// <summary>
28        /// Unique instance of <seealso cref="FsComFramework.Logger.Logger"/>.
29        /// </summary>
30        private static volatile Logger instance = null;
31
32        /// <summary>
33        /// Synchronisation object.
34        /// </summary>
35        private static object _syncRoot = new Object();
36
37        /// <summary>
38        /// Internal ctor.
39        /// </summary>
40        private Logger()
41        {
42        }
43
44        /// <summary>
45        /// Instance accessor.
46        /// </summary>
47        /// <example>
48        /// The code below shows how to do Logger access:
49        /// <code>
50        /// public GenericSAFrm()
51        /// {
52        ///     // Initialize logger.
53        ///     Logger.Instance.Initialize("GenericSA");
54        ///
55        ///     InitializeComponent();
56        /// }
57        /// </code>
58        /// </example>
59        public static Logger Instance
60        {
61            get
62            {
63                lock (_syncRoot)
64                {
65                    if (instance == null)
66                    {
67                        instance = new Logger();
68                    }
69
70                    return instance;
71                }
72            }
73        }
74
75        #endregion Thread-safe Singleton implementation
76
77        ///////////////////////////////////////////////////////////////////////
78        #region Logger methods.
79
80        /// <summary>
81        /// Gets the Debug level status: true if set, false otherwise.
82        /// </summary>
83        public bool IsDebugLevelSet { get { return _logger.IsDebugEnabled; } }
84
85        /// <summary>
86        /// Gets the Info level status: true if set, false otherwise.
87        /// </summary>
88        public bool IsInfoLevelSet { get { return _logger.IsInfoEnabled; } }
89
90        /// <summary>
91        /// Gets the Warning level status: true if set, false otherwise.
92        /// </summary>
93        public bool IsWarningLevelSet { get { return _logger.IsWarnEnabled; } }
94
95        /// <summary>
96        /// Initialize the logger component.
97        /// </summary>
98        /// <param name="name">Logger name - see application configuation file, log4net XML element.</param>
99        /// <example>
100        /// The code below shows how to do Logger access:
101        /// <code>
102        /// public GenericSAFrm()
103        /// {
104        ///     // Initialize logger.
105        ///     Logger.Instance.Initialize("GenericSA");
106        ///
107        ///     InitializeComponent();
108        /// }
109        /// </code>
110        /// </example>
111        public void Initialize(string name)
112        {
113            if (string.IsNullOrEmpty(name))
114            {
115                _logger = LogManager.GetLogger("TraceAppender");
116                XmlConfigurator.Configure();
117            }
118            else
119            {
120                _logger = LogManager.GetLogger(name);
121                XmlConfigurator.Configure();
122            }
123        }
124
125        /// <summary>
126        /// Log information message.
127        /// </summary>
128        /// <param name="fmt">Message format - <see>IFormater</see>.</param>
129        /// <param name="args">List of argument to pass to the formater.</param>         
130        /// <example>
131        /// The code below shows how to do Logger access:
132        /// <code>
133        /// if (Logger.Instance.IsInfoLevelSet) Logger.Instance.InfoLogger("GenericSAFrm.Window_Loaded: All is ok, continue!");
134        /// </code>
135        /// </example>
136        public void InfoLogger(string fmt, params object[] args)
137        {
138            if (_logger.IsInfoEnabled) _logger.InfoFormat(fmt, args);
139        }
140
141        /// <summary>
142        /// Log warning message.
143        /// </summary>
144        /// <param name="fmt">Message format - <see>IFormater</see>.</param>
145        /// <param name="args">List of argument to pass to the formater.</param>         
146        /// <example>
147        /// The code below shows how to do Logger access:
148        /// <code>
149        /// Logger.Instance.WarningLogger("GenericSAFrm.Window_Loaded: Configuration mismatch!");
150        /// </code>
151        /// </example>
152        public void WarningLogger(string fmt, params object[] args)
153        {
154            if (_logger.IsWarnEnabled) _logger.WarnFormat(fmt, args);
155        }
156
157        /// <summary>
158        /// Log error message.
159        /// </summary>
160        /// <param name="fmt">Message format - <see>IFormater</see>.</param>
161        /// <param name="args">List of argument to pass to the formater.</param>         
162        /// <example>
163        /// The code below shows how to do Logger access:
164        /// <code>
165        /// Logger.Instance.ErrorLogger("GenericSAFrm.Window_Loaded: Wrong parameters.");
166        /// </code>
167        /// </example>
168        public void ErrorLogger(string fmt, params object[] args)
169        {
170            if (_logger.IsErrorEnabled) _logger.ErrorFormat(fmt, args);
171        }
172
173        /// <summary>
174        /// Log debug message.
175        /// <remarks>Visible only if level in root XML element is set to ALL or DEBUG.</remarks>
176        /// </summary>
177        /// <param name="fmt">Message format - <see>IFormater</see>.</param>
178        /// <param name="args">List of argument to pass to the formater.</param>         
179        /// <example>
180        /// The code below shows how to do Logger access:
181        /// <code>
182        /// if (Logger.Instance.IsDebugLevelSet) Logger.Instance.DebugLogger("GenericSAFrm.Window_Loaded: Parameters {0} is validated.", counter);
183        /// </code>
184        /// </example>
185        public void DebugLogger(string fmt, params object[] args)
186        {
187            if (_logger.IsDebugEnabled) _logger.DebugFormat(fmt, args);
188        }
189
190        #endregion Logger methods.
191
192        ///////////////////////////////////////////////////////////////////////
193        #region Internal method helpers.
194
195        #endregion Internal method helpers.
196    }
197}
Note: See TracBrowser for help on using the repository browser.