~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/Support/InMemoryLogFactory.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Text;
 
4
using ServiceStack.Logging;
 
5
 
 
6
namespace ServiceStack.Common.Support
 
7
{
 
8
    /// <summary>
 
9
    /// Note: InMemoryLog keeps all logs in memory, so don't use it long running exceptions
 
10
    /// 
 
11
    /// Returns a thread-safe InMemoryLog which you can use while *TESTING*
 
12
    /// to provide a detailed analysis of your logs.
 
13
    /// </summary>
 
14
    public class InMemoryLogFactory
 
15
        : ILogFactory
 
16
    {
 
17
        public ILog GetLogger(Type type)
 
18
        {
 
19
            return new InMemoryLog(type.Name);
 
20
        }
 
21
 
 
22
        public ILog GetLogger(string typeName)
 
23
        {
 
24
            return new InMemoryLog(typeName);
 
25
        }
 
26
    }
 
27
 
 
28
    public class InMemoryLog
 
29
        : ILog
 
30
    {
 
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; }
 
44
 
 
45
        public InMemoryLog(string loggerName)
 
46
        {
 
47
            this.LoggerName = loggerName;
 
48
            this.CombinedLog = new StringBuilder();
 
49
 
 
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>();
 
60
        }
 
61
 
 
62
        public bool HasExceptions
 
63
        {
 
64
            get
 
65
            {
 
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;
 
71
            }
 
72
        }
 
73
 
 
74
        private void AppendToLog(ICollection<string> logEntries, string format, params object[] args)
 
75
        {
 
76
            if (format == null) return;
 
77
            AppendToLog(logEntries, string.Format(format, args));
 
78
        }
 
79
 
 
80
        private void AppendToLog(ICollection<string> logEntries, object message)
 
81
        {
 
82
            if (message == null) return;
 
83
            AppendToLog(logEntries, message.ToString());
 
84
        }
 
85
 
 
86
        private void AppendToLog(
 
87
            ICollection<string> logEntries, 
 
88
            ICollection<Exception> logExceptions, 
 
89
            object message, Exception ex)
 
90
        {
 
91
            if (ex != null)
 
92
            {
 
93
                lock (syncLock)
 
94
                {
 
95
                    logExceptions.Add(ex);
 
96
                }
 
97
            }
 
98
            if (message == null) return;
 
99
            AppendToLog(logEntries, message.ToString());
 
100
        }
 
101
 
 
102
        private void AppendToLog(ICollection<string> logEntries, string message)
 
103
        {
 
104
            lock (this)
 
105
            {
 
106
                logEntries.Add(message);
 
107
                CombinedLog.AppendLine(message);
 
108
            }
 
109
        }
 
110
 
 
111
        public void Debug(object message)
 
112
        {
 
113
            AppendToLog(DebugEntries, message);
 
114
        }
 
115
 
 
116
        public void Debug(object message, Exception exception)
 
117
        {
 
118
            AppendToLog(DebugEntries, DebugExceptions, message, exception);
 
119
        }
 
120
 
 
121
        public void DebugFormat(string format, params object[] args)
 
122
        {
 
123
            AppendToLog(DebugEntries, format, args);
 
124
        }
 
125
 
 
126
        public void Error(object message)
 
127
        {
 
128
            AppendToLog(ErrorEntries, message);
 
129
        }
 
130
 
 
131
        public void Error(object message, Exception exception)
 
132
        {
 
133
            AppendToLog(ErrorEntries, ErrorExceptions, message, exception);
 
134
        }
 
135
 
 
136
        public void ErrorFormat(string format, params object[] args)
 
137
        {
 
138
            AppendToLog(ErrorEntries, format, args);
 
139
        }
 
140
 
 
141
        public void Fatal(object message)
 
142
        {
 
143
            AppendToLog(FatalEntries, message);
 
144
        }
 
145
 
 
146
        public void Fatal(object message, Exception exception)
 
147
        {
 
148
            AppendToLog(FatalEntries, FatalExceptions, message, exception);
 
149
        }
 
150
 
 
151
        public void FatalFormat(string format, params object[] args)
 
152
        {
 
153
            AppendToLog(FatalEntries, format, args);
 
154
        }
 
155
 
 
156
        public void Info(object message)
 
157
        {
 
158
            AppendToLog(InfoEntries, message);
 
159
        }
 
160
 
 
161
        public void Info(object message, Exception exception)
 
162
        {
 
163
            AppendToLog(InfoEntries, InfoExceptions, message, exception);
 
164
        }
 
165
 
 
166
        public void InfoFormat(string format, params object[] args)
 
167
        {
 
168
            AppendToLog(InfoEntries, format, args);
 
169
        }
 
170
 
 
171
        public void Warn(object message)
 
172
        {
 
173
            AppendToLog(WarnEntries, message);
 
174
        }
 
175
 
 
176
        public void Warn(object message, Exception exception)
 
177
        {
 
178
            AppendToLog(WarnEntries, WarnExceptions, message, exception);
 
179
        }
 
180
 
 
181
        public void WarnFormat(string format, params object[] args)
 
182
        {
 
183
            AppendToLog(WarnEntries, format, args);
 
184
        }
 
185
 
 
186
        public bool IsDebugEnabled
 
187
        {
 
188
            get { return true; }
 
189
        }
 
190
    }
 
191
}
 
 
b'\\ No newline at end of file'