Skip to content

Commit 47e8cf1

Browse files
author
Mark Swiatek
committed
Add constructor to Log4NetFactory that takes an external log4net configuration file.
Add guards (if IsXxxxEnabled) to log4net log commands for better performance. See http://logging.apache.org/log4net/release/faq.html#perf-not-logging
1 parent cc4c858 commit 47e8cf1

5 files changed

Lines changed: 87 additions & 24 deletions

File tree

src/ServiceStack.Logging.Log4Net/Log4NetFactory.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
2-
using ServiceStack.Logging;
2+
using System.IO;
33

44
namespace ServiceStack.Logging.Log4Net
55
{
66
/// <summary>
77
/// ILogFactory that creates an Log4Net ILog logger
88
/// </summary>
9-
public class Log4NetFactory : ServiceStack.Logging.ILogFactory
9+
public class Log4NetFactory : ILogFactory
1010
{
1111
/// <summary>
1212
/// Initializes a new instance of the <see cref="Log4NetFactory"/> class.
@@ -25,6 +25,23 @@ public Log4NetFactory(bool configureLog4Net)
2525
}
2626
}
2727

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="Log4NetFactory"/> class.
30+
/// </summary>
31+
/// <param name="log4NetConfigurationFile">The log4 net configuration file to load and watch. If not found configures from App.Config.</param>
32+
public Log4NetFactory(string log4NetConfigurationFile)
33+
{
34+
//Restart logging if necessary
35+
log4net.Repository.ILoggerRepository rootRepository = log4net.LogManager.GetRepository();
36+
if (rootRepository != null)
37+
rootRepository.Shutdown();
38+
39+
if (File.Exists(log4NetConfigurationFile))
40+
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigurationFile));
41+
else
42+
log4net.Config.XmlConfigurator.Configure();
43+
}
44+
2845
/// <summary>
2946
/// Gets the logger.
3047
/// </summary>

src/ServiceStack.Logging.Log4Net/Log4NetLogger.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using System;
2-
using ServiceStack.Logging;
32

43
namespace ServiceStack.Logging.Log4Net
54
{
65
/// <summary>
76
/// Wrapper over the log4net.1.2.10 and above logger
87
/// </summary>
9-
public class Log4NetLogger : ServiceStack.Logging.ILog
8+
public class Log4NetLogger : ILog
109
{
11-
private readonly log4net.ILog log;
10+
private readonly log4net.ILog _log;
1211

1312
public Log4NetLogger(string typeName)
1413
{
15-
log = log4net.LogManager.GetLogger(typeName);
14+
_log = log4net.LogManager.GetLogger(typeName);
1615
}
1716

1817
/// <summary>
@@ -21,18 +20,19 @@ public Log4NetLogger(string typeName)
2120
/// <param name="type">The type.</param>
2221
public Log4NetLogger(Type type)
2322
{
24-
log = log4net.LogManager.GetLogger(type);
23+
_log = log4net.LogManager.GetLogger(type);
2524
}
2625

27-
public bool IsDebugEnabled { get { return log.IsDebugEnabled; } }
26+
public bool IsDebugEnabled { get { return _log.IsDebugEnabled; } }
2827

2928
/// <summary>
3029
/// Logs a Debug message.
3130
/// </summary>
3231
/// <param name="message">The message.</param>
3332
public void Debug(object message)
3433
{
35-
log.Debug(message);
34+
if (_log.IsDebugEnabled)
35+
_log.Debug(message);
3636
}
3737

3838
/// <summary>
@@ -42,7 +42,8 @@ public void Debug(object message)
4242
/// <param name="exception">The exception.</param>
4343
public void Debug(object message, Exception exception)
4444
{
45-
log.Debug(message, exception);
45+
if (_log.IsDebugEnabled)
46+
_log.Debug(message, exception);
4647
}
4748

4849
/// <summary>
@@ -52,7 +53,8 @@ public void Debug(object message, Exception exception)
5253
/// <param name="args">The args.</param>
5354
public void DebugFormat(string format, params object[] args)
5455
{
55-
log.DebugFormat(format, args);
56+
if (_log.IsDebugEnabled)
57+
_log.DebugFormat(format, args);
5658
}
5759

5860
/// <summary>
@@ -61,7 +63,8 @@ public void DebugFormat(string format, params object[] args)
6163
/// <param name="message">The message.</param>
6264
public void Error(object message)
6365
{
64-
log.Error(message);
66+
if (_log.IsErrorEnabled)
67+
_log.Error(message);
6568
}
6669

6770
/// <summary>
@@ -71,7 +74,8 @@ public void Error(object message)
7174
/// <param name="exception">The exception.</param>
7275
public void Error(object message, Exception exception)
7376
{
74-
log.Error(message, exception);
77+
if (_log.IsErrorEnabled)
78+
_log.Error(message, exception);
7579
}
7680

7781
/// <summary>
@@ -81,7 +85,8 @@ public void Error(object message, Exception exception)
8185
/// <param name="args">The args.</param>
8286
public void ErrorFormat(string format, params object[] args)
8387
{
84-
log.ErrorFormat(format, args);
88+
if (_log.IsErrorEnabled)
89+
_log.ErrorFormat(format, args);
8590
}
8691

8792
/// <summary>
@@ -90,7 +95,8 @@ public void ErrorFormat(string format, params object[] args)
9095
/// <param name="message">The message.</param>
9196
public void Fatal(object message)
9297
{
93-
log.Fatal(message);
98+
if (_log.IsFatalEnabled)
99+
_log.Fatal(message);
94100
}
95101

96102
/// <summary>
@@ -100,7 +106,8 @@ public void Fatal(object message)
100106
/// <param name="exception">The exception.</param>
101107
public void Fatal(object message, Exception exception)
102108
{
103-
log.Fatal(message, exception);
109+
if (_log.IsFatalEnabled)
110+
_log.Fatal(message, exception);
104111
}
105112

106113
/// <summary>
@@ -110,7 +117,8 @@ public void Fatal(object message, Exception exception)
110117
/// <param name="args">The args.</param>
111118
public void FatalFormat(string format, params object[] args)
112119
{
113-
log.FatalFormat(format, args);
120+
if (_log.IsFatalEnabled)
121+
_log.FatalFormat(format, args);
114122
}
115123

116124
/// <summary>
@@ -119,7 +127,8 @@ public void FatalFormat(string format, params object[] args)
119127
/// <param name="message">The message.</param>
120128
public void Info(object message)
121129
{
122-
log.Info(message);
130+
if (_log.IsInfoEnabled)
131+
_log.Info(message);
123132
}
124133

125134
/// <summary>
@@ -129,7 +138,8 @@ public void Info(object message)
129138
/// <param name="exception">The exception.</param>
130139
public void Info(object message, Exception exception)
131140
{
132-
log.Info(message, exception);
141+
if (_log.IsInfoEnabled)
142+
_log.Info(message, exception);
133143
}
134144

135145
/// <summary>
@@ -139,7 +149,8 @@ public void Info(object message, Exception exception)
139149
/// <param name="args">The args.</param>
140150
public void InfoFormat(string format, params object[] args)
141151
{
142-
log.InfoFormat(format, args);
152+
if (_log.IsInfoEnabled)
153+
_log.InfoFormat(format, args);
143154
}
144155

145156
/// <summary>
@@ -148,7 +159,8 @@ public void InfoFormat(string format, params object[] args)
148159
/// <param name="message">The message.</param>
149160
public void Warn(object message)
150161
{
151-
log.Warn(message);
162+
if (_log.IsWarnEnabled)
163+
_log.Warn(message);
152164
}
153165

154166
/// <summary>
@@ -158,7 +170,8 @@ public void Warn(object message)
158170
/// <param name="exception">The exception.</param>
159171
public void Warn(object message, Exception exception)
160172
{
161-
log.Warn(message, exception);
173+
if (_log.IsWarnEnabled)
174+
_log.Warn(message, exception);
162175
}
163176

164177
/// <summary>
@@ -168,7 +181,8 @@ public void Warn(object message, Exception exception)
168181
/// <param name="args">The args.</param>
169182
public void WarnFormat(string format, params object[] args)
170183
{
171-
log.WarnFormat(format, args);
184+
if (_log.IsWarnEnabled)
185+
_log.WarnFormat(format, args);
172186
}
173187
}
174-
}
188+
}

tests/ServiceStack.Logging.Tests/ServiceStack.Logging.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
</ProjectReference>
159159
</ItemGroup>
160160
<ItemGroup>
161+
<Content Include="log4net.Test.config">
162+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
163+
</Content>
161164
<None Include="packages.config" />
162165
</ItemGroup>
163166
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

tests/ServiceStack.Logging.Tests/UnitTests/Log4NetFactoryTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using NUnit.Framework;
23
using ServiceStack.Logging.Log4Net;
34

@@ -19,5 +20,18 @@ public void Log4NetFactoryTest()
1920
Assert.IsNotNull(log);
2021
Assert.IsNotNull(log as Log4NetLogger);
2122
}
23+
24+
[Test]
25+
public void Log4NetFactoryTestWithExistingConfigFile()
26+
{
27+
const string configFile = "log4net.Test.config";
28+
Assert.IsTrue(File.Exists(configFile), "Test setup failure. Required log4net config file is missing.");
29+
30+
Log4NetFactory factory = new Log4NetFactory(configFile);
31+
32+
ILog log = factory.GetLogger(GetType());
33+
Assert.IsNotNull(log);
34+
Assert.IsNotNull(log as Log4NetLogger);
35+
}
2236
}
2337
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<log4net debug="false">
2+
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
3+
4+
<!-- ConsoleAppender uses PatternLayout -->
5+
<layout type="log4net.Layout.PatternLayout">
6+
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
7+
</layout>
8+
</appender>
9+
10+
<!-- Set root logger level to DEBUG and its only appender to DebugFileAppender -->
11+
<root>
12+
<level value="DEBUG" />
13+
</root>
14+
15+
</log4net>

0 commit comments

Comments
 (0)