Skip to content

Commit fccd511

Browse files
committed
Added new StringBuilderLog for capturing logs in a shared StringBuilder
1 parent 939e48c commit fccd511

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#if !NETFX_CORE
2+
using System;
3+
using System.Text;
4+
5+
namespace ServiceStack.Logging.Support.Logging
6+
{
7+
/// <summary>
8+
/// StringBuilderLog writes to shared StringBuffer.
9+
/// Made public so its testable
10+
/// </summary>
11+
public class StringBuilderLogFactory : ILogFactory
12+
{
13+
private StringBuilder sb;
14+
15+
public StringBuilderLogFactory()
16+
{
17+
sb = new StringBuilder();
18+
}
19+
20+
public ILog GetLogger(Type type)
21+
{
22+
return new StringBuilderLog(type, sb);
23+
}
24+
25+
public ILog GetLogger(string typeName)
26+
{
27+
return new StringBuilderLog(typeName, sb);
28+
}
29+
30+
public string GetLogs()
31+
{
32+
lock (sb)
33+
return sb.ToString();
34+
}
35+
36+
public void ClearLogs()
37+
{
38+
lock (sb)
39+
sb.Remove(0, sb.Length - 1);
40+
}
41+
}
42+
43+
public class StringBuilderLog : ILog
44+
{
45+
const string DEBUG = "DEBUG: ";
46+
const string ERROR = "ERROR: ";
47+
const string FATAL = "FATAL: ";
48+
const string INFO = "INFO: ";
49+
const string WARN = "WARN: ";
50+
private readonly StringBuilder logs;
51+
52+
public StringBuilderLog(string type, StringBuilder logs)
53+
{
54+
this.logs = logs;
55+
}
56+
57+
public StringBuilderLog(Type type, StringBuilder logs)
58+
{
59+
this.logs = logs;
60+
}
61+
62+
public bool IsDebugEnabled { get { return true; } }
63+
64+
/// <summary>
65+
/// Logs the specified message.
66+
/// </summary>
67+
/// <param name="message">The message.</param>
68+
/// <param name="exception">The exception.</param>
69+
private void Log(object message, Exception exception)
70+
{
71+
string msg = message == null ? string.Empty : message.ToString();
72+
if (exception != null)
73+
{
74+
msg += ", Exception: " + exception.Message;
75+
}
76+
lock (logs)
77+
logs.AppendLine(msg);
78+
}
79+
80+
/// <summary>
81+
/// Logs the format.
82+
/// </summary>
83+
/// <param name="message">The message.</param>
84+
/// <param name="args">The args.</param>
85+
private void LogFormat(object message, params object[] args)
86+
{
87+
string msg = message == null ? string.Empty : message.ToString();
88+
lock (logs)
89+
{
90+
logs.AppendFormat(msg, args);
91+
logs.AppendLine();
92+
}
93+
}
94+
95+
/// <summary>
96+
/// Logs the specified message.
97+
/// </summary>
98+
/// <param name="message">The message.</param>
99+
private void Log(object message)
100+
{
101+
string msg = message == null ? string.Empty : message.ToString();
102+
lock (logs)
103+
{
104+
logs.AppendLine(msg);
105+
}
106+
}
107+
108+
public void Debug(object message, Exception exception)
109+
{
110+
Log(DEBUG + message, exception);
111+
}
112+
113+
public void Debug(object message)
114+
{
115+
Log(DEBUG + message);
116+
}
117+
118+
public void DebugFormat(string format, params object[] args)
119+
{
120+
LogFormat(DEBUG + format, args);
121+
}
122+
123+
public void Error(object message, Exception exception)
124+
{
125+
Log(ERROR + message, exception);
126+
}
127+
128+
public void Error(object message)
129+
{
130+
Log(ERROR + message);
131+
}
132+
133+
public void ErrorFormat(string format, params object[] args)
134+
{
135+
LogFormat(ERROR + format, args);
136+
}
137+
138+
public void Fatal(object message, Exception exception)
139+
{
140+
Log(FATAL + message, exception);
141+
}
142+
143+
public void Fatal(object message)
144+
{
145+
Log(FATAL + message);
146+
}
147+
148+
public void FatalFormat(string format, params object[] args)
149+
{
150+
LogFormat(FATAL + format, args);
151+
}
152+
153+
public void Info(object message, Exception exception)
154+
{
155+
Log(INFO + message, exception);
156+
}
157+
158+
public void Info(object message)
159+
{
160+
Log(INFO + message);
161+
}
162+
163+
public void InfoFormat(string format, params object[] args)
164+
{
165+
LogFormat(INFO + format, args);
166+
}
167+
168+
public void Warn(object message, Exception exception)
169+
{
170+
Log(WARN + message, exception);
171+
}
172+
173+
public void Warn(object message)
174+
{
175+
Log(WARN + message);
176+
}
177+
178+
public void WarnFormat(string format, params object[] args)
179+
{
180+
LogFormat(WARN + format, args);
181+
}
182+
}
183+
}
184+
#endif

src/ServiceStack.Interfaces/ServiceStack.Interfaces.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@
309309
<Compile Include="Logging\Support\Logging\ConsoleLogger.cs">
310310
<SubType>Code</SubType>
311311
</Compile>
312+
<Compile Include="Logging\Support\Logging\StringBuilderLog.cs" />
312313
<Compile Include="Logging\Support\Logging\DebugLogFactory.cs">
313314
<SubType>Code</SubType>
314315
</Compile>

0 commit comments

Comments
 (0)