-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathILog.cs
136 lines (112 loc) · 3.97 KB
/
ILog.cs
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
128
129
130
131
132
133
134
135
136
using System;
using System.Diagnostics;
namespace WPFMediaKit
{
/// <summary>
/// A logger.
/// </summary>
public interface ILog
{
/// <summary>
/// If the info level is enabled.
/// </summary>
bool IsInfoEnabled { get; }
/// <summary>
/// If the debug level is enabled.
/// </summary>
bool IsDebugEnabled { get; }
/// <summary>
/// Logs the error message with a possible exception.
/// </summary>
/// <param name="exception">The exception. May be null.</param>
/// <param name="message">A (formatted) message.</param>
/// <param name="args">Arguments to the formatted message. May be missing.
void Error(Exception exception, string message, params object[] args);
/// <summary>
/// Logs the warning message.
/// </summary>
/// <param name="message">A (formatted) message.</param>
/// <param name="args">Arguments to the formatted message. May be missing.
void Warn(string message, params object[] args);
/// <summary>
/// Logs the info message.
/// </summary>
/// <param name="message">A (formatted) message.</param>
/// <param name="args">Arguments to the formatted message. May be missing.
void Info(string message, params object[] args);
/// <summary>
/// Logs the debug message.
/// </summary>
/// <param name="message">A (formatted) message.</param>
/// <param name="args">Arguments to the formatted message. May be missing.
void Debug(string message, params object[] args);
}
public static class ILogExtensions
{
/// <summary>
/// Log the error without an exception.
/// </summary>
public static void Error(this ILog thiz, string message, params object[] args)
=> thiz.Error(null, message, args);
}
/// <summary>
/// Logging to <see cref="System.Diagnostics.Trace"/>.
/// </summary>
public class DebugTraceLog : ILog
{
private string _name;
public DebugTraceLog(string name)
{
_name = name;
}
public virtual bool IsInfoEnabled => true;
public virtual bool IsDebugEnabled => true;
public virtual void Error(Exception exception, string message, params object[] args)
{
Trace.TraceError(AddName(message), args);
if (exception != null)
Trace.TraceError(exception.ToString());
}
public virtual void Warn(string message, params object[] args)
{
Trace.TraceWarning(AddName(message), args);
}
public virtual void Info(string message, params object[] args)
{
if (!IsInfoEnabled)
return;
Trace.TraceInformation(AddName(message), args);
}
public virtual void Debug(string message, params object[] args)
{
if (!IsDebugEnabled)
return;
Trace.WriteLine(string.Format(AddName(message), args));
}
protected virtual string AddName(string message)
{
if (string.IsNullOrEmpty(_name))
return message;
return _name + " - " + message;
}
}
/// <summary>
/// No-op implementation of ILog.
/// </summary>
public class NullLog : ILog
{
private static NullLog _instance;
public static NullLog Instance
=> _instance != null ? _instance : (_instance = new NullLog());
public bool IsInfoEnabled => false;
public virtual bool IsDebugEnabled => false;
public void Error(Exception exception, string message, params object[] args)
{ }
public void Info(string message, params object[] args)
{ }
public void Debug(string message, params object[] args)
{ }
public void Warn(string message, params object[] args)
{ }
}
}