1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Duplicati.Library.Logging
{
/// <summary>
/// The different types of messages
/// </summary>
public enum LogMessageType
{
/// <summary>
/// The message is a profiling message
/// </summary>
Profiling,
/// <summary>
/// The message is informative but does not indicate problems
/// </summary>
Information,
/// <summary>
/// The message is a warning, meaning that later errors may be releated to this message
/// </summary>
Warning,
/// <summary>
/// The message indicates an error
/// </summary>
Error,
}
/// <summary>
/// This static class is used to write log messages
/// </summary>
public static class Log
{
/// <summary>
/// Static lock object to provide thread safe logging
/// </summary>
private static object m_lock = new object();
/// <summary>
/// The log destination, may be null
/// </summary>
private static ILog m_log = null;
/// <summary>
/// The minimum level of logged messages
/// </summary>
private static LogMessageType m_logLevel = LogMessageType.Error;
/// <summary>
/// Writes a message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="type">The type of the message</param>
public static void WriteMessage(string message, LogMessageType type)
{
WriteMessage(message, type, null);
}
/// <summary>
/// Writes a message to the current log destination
/// </summary>
/// <param name="message">The message to write</param>
/// <param name="type">The type of the message</param>
/// <param name="ex">An exception value</param>
public static void WriteMessage(string message, LogMessageType type, Exception ex)
{
if (m_log == null)
return;
lock (m_lock)
if (m_log == null)
return;
else
{
if (type >= m_logLevel)
m_log.WriteMessage(message, type, ex);
}
}
/// <summary>
/// Gets or sets the level of log messages to write to the CurrentLog
/// </summary>
public static LogMessageType LogLevel
{
get { return m_logLevel; }
set
{
lock (m_lock)
m_logLevel = value;
}
}
/// <summary>
/// Gets or sets the current log destination
/// </summary>
public static ILog CurrentLog
{
get
{
return m_log;
}
set
{
lock (m_lock)
m_log = value;
}
}
}
}
|