2
using System.Collections.Generic;
4
using ServiceStack.Logging;
6
namespace ServiceStack.Common.Support
9
/// Note: InMemoryLog keeps all logs in memory, so don't use it long running exceptions
11
/// Returns a thread-safe InMemoryLog which you can use while *TESTING*
12
/// to provide a detailed analysis of your logs.
14
public class InMemoryLogFactory
17
public ILog GetLogger(Type type)
19
return new InMemoryLog(type.Name);
22
public ILog GetLogger(string typeName)
24
return new InMemoryLog(typeName);
28
public class InMemoryLog
31
private readonly object syncLock = new object();
32
public string LoggerName { get; private set; }
33
public StringBuilder CombinedLog { get; private set; }
34
public List<string> DebugEntries { get; set; }
35
public List<Exception> DebugExceptions { get; set; }
36
public List<string> InfoEntries { get; set; }
37
public List<Exception> InfoExceptions { get; set; }
38
public List<string> WarnEntries { get; set; }
39
public List<Exception> WarnExceptions { get; set; }
40
public List<string> ErrorEntries { get; set; }
41
public List<Exception> ErrorExceptions { get; set; }
42
public List<string> FatalEntries { get; set; }
43
public List<Exception> FatalExceptions { get; set; }
45
public InMemoryLog(string loggerName)
47
this.LoggerName = loggerName;
48
this.CombinedLog = new StringBuilder();
50
this.DebugEntries = new List<string>();
51
this.DebugExceptions = new List<Exception>();
52
this.InfoEntries = new List<string>();
53
this.InfoExceptions = new List<Exception>();
54
this.WarnEntries = new List<string>();
55
this.WarnExceptions = new List<Exception>();
56
this.ErrorEntries = new List<string>();
57
this.ErrorExceptions = new List<Exception>();
58
this.FatalEntries = new List<string>();
59
this.FatalExceptions = new List<Exception>();
62
public bool HasExceptions
66
return this.DebugExceptions.Count > 0
67
|| this.InfoExceptions.Count > 0
68
|| this.WarnExceptions.Count > 0
69
|| this.ErrorExceptions.Count > 0
70
|| this.FatalExceptions.Count > 0;
74
private void AppendToLog(ICollection<string> logEntries, string format, params object[] args)
76
if (format == null) return;
77
AppendToLog(logEntries, string.Format(format, args));
80
private void AppendToLog(ICollection<string> logEntries, object message)
82
if (message == null) return;
83
AppendToLog(logEntries, message.ToString());
86
private void AppendToLog(
87
ICollection<string> logEntries,
88
ICollection<Exception> logExceptions,
89
object message, Exception ex)
95
logExceptions.Add(ex);
98
if (message == null) return;
99
AppendToLog(logEntries, message.ToString());
102
private void AppendToLog(ICollection<string> logEntries, string message)
106
logEntries.Add(message);
107
CombinedLog.AppendLine(message);
111
public void Debug(object message)
113
AppendToLog(DebugEntries, message);
116
public void Debug(object message, Exception exception)
118
AppendToLog(DebugEntries, DebugExceptions, message, exception);
121
public void DebugFormat(string format, params object[] args)
123
AppendToLog(DebugEntries, format, args);
126
public void Error(object message)
128
AppendToLog(ErrorEntries, message);
131
public void Error(object message, Exception exception)
133
AppendToLog(ErrorEntries, ErrorExceptions, message, exception);
136
public void ErrorFormat(string format, params object[] args)
138
AppendToLog(ErrorEntries, format, args);
141
public void Fatal(object message)
143
AppendToLog(FatalEntries, message);
146
public void Fatal(object message, Exception exception)
148
AppendToLog(FatalEntries, FatalExceptions, message, exception);
151
public void FatalFormat(string format, params object[] args)
153
AppendToLog(FatalEntries, format, args);
156
public void Info(object message)
158
AppendToLog(InfoEntries, message);
161
public void Info(object message, Exception exception)
163
AppendToLog(InfoEntries, InfoExceptions, message, exception);
166
public void InfoFormat(string format, params object[] args)
168
AppendToLog(InfoEntries, format, args);
171
public void Warn(object message)
173
AppendToLog(WarnEntries, message);
176
public void Warn(object message, Exception exception)
178
AppendToLog(WarnEntries, WarnExceptions, message, exception);
181
public void WarnFormat(string format, params object[] args)
183
AppendToLog(WarnEntries, format, args);
186
public bool IsDebugEnabled
b'\\ No newline at end of file'